可变名称中的单词和数字之间需要下划线吗?

发布于 2025-02-07 14:03:31 字数 713 浏览 0 评论 0原文

我有2个变量,city1city2,如下所示:

city1 = "New York" 
city2 = "Los Angeles"

根据函数和变量名称 in pep 8 - python代码的样式指南,在变量名称中的单词之间需要下划线_,如下所示:

first_name = "John"
last_name = "Tom"

现在,在变量名称中的单词和数字之间也需要下划线。如下所示?

city_1 = "New York" 
city_2 = "Los Angeles"

I have 2 variables, city1 and city2 as shown below:

city1 = "New York" 
city2 = "Los Angeles"

According to Function and Variable Names in PEP 8 – Style Guide for Python Code, an underscore _ is needed between the words in a variable name as shown below:

first_name = "John"
last_name = "Tom"

Now, is underscore also needed between the word and number in a variable name as shown below?

city_1 = "New York" 
city_2 = "Los Angeles"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2025-02-14 14:03:31

来自pep8:

函数名称应该是小写的,并根据必要的下划线分隔以提高可读性。

变量名称遵循与函数名称相同的约定。

因此,下划线可以提高可读性,但是您可以根据需要自由地做。

longvariable = 0
long_variable = 1
city1 = 2
city_2 = 3

最主要的是在您的代码样式和组织的风格中保持一致。

From PEP8:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

So underscores are there to improve readability, but you are free to do as you want.

longvariable = 0
long_variable = 1
city1 = 2
city_2 = 3

The main thing is to stay consistent within your code style and your organisation's style.

纵性 2025-02-14 14:03:31

Python命名约定未指定您是否需要通过下划线将数字与字母分开。这是一个意见问题。这是我的。

我没有在字母和数字之间使用分离器。因此,我使用:

city1 = "New York" 
city2 = "Los Angeles"

似乎高度科学的Google搜索同意:
file:py“ test1”结果
file:py“ test_1” 结果

712,000用户怎么会错?但是说真的,这是一个数量级的差异!

一些相关注释:

  • 当需要数字扩展时,使用列表或词典,这些列表或词典更可扩展,并且允许迭代,循环,综合等 - 所有可以帮助保持代码不重复的所有内容:
# Lists:
cities = ['New York', 'Los Angeles']
print(cities[0])
# New York

cities.append('Boston')
cities = [c.lower() for c in cities]
print(cities[2])
# boston

# Dictionaries:
city  = {'src': 'New York', 'dest': 'Los Angeles'}
print(city['src'])
# New York
  • 避免使用相对无信息的扩展,例如<12,除非它们属于用户的知识域 。例如,read1read2对生物学家的特定含义,他们是下一代测序分析的用户。但这很少见。即使那样,列表还是一个更好的主意(因为比您想象的要早,还会有read3read4)。也许src_citydest_city可以是更好的选择,city> city1city2

Python naming conventions do not specify whether or not you need to separate numbers from letters by an underscore. This is a matter of opinion. Here is mine.

I use no separator between letters and numbers for brevity. Thus, I use:

city1 = "New York" 
city2 = "Los Angeles"

It seems that the highly scientific google search agrees:
file:py "test1": About 712,000 results
file:py "test_1": About 66,800 results

How can 712,000 users be wrong? But seriously, that's an order of magnitude difference!

A few related notes:

  • When numeric extensions are needed, use lists or dictionaries, which are more extensible, and which allow iterations, loops, comprehensions, etc - everything that can help keep the code non repetitive:
# Lists:
cities = ['New York', 'Los Angeles']
print(cities[0])
# New York

cities.append('Boston')
cities = [c.lower() for c in cities]
print(cities[2])
# boston

# Dictionaries:
city  = {'src': 'New York', 'dest': 'Los Angeles'}
print(city['src'])
# New York
  • Avoid relatively uninformative extensions such as 1 or 2, unless they are part of the knowledge domain of the users. For example, read1 and read2 have specific meaning for biologists, who are the users in the analysis of next-generation sequencing. But that's rare. And even then, lists are a better idea (because sooner than you think, there will also be read3 and read4). Perhaps src_city and dest_city can be better choices and city1 and city2.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文