可变名称中的单词和数字之间需要下划线吗?
我有2个变量,city1
和city2
,如下所示:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自pep8:
因此,下划线可以提高可读性,但是您可以根据需要自由地做。
最主要的是在您的代码样式和组织的风格中保持一致。
From PEP8:
So underscores are there to improve readability, but you are free to do as you want.
The main thing is to stay consistent within your code style and your organisation's style.
Python命名约定未指定您是否需要通过下划线将数字与字母分开。这是一个意见问题。这是我的。
我没有在字母和数字之间使用分离器。因此,我使用:
似乎高度科学的Google搜索同意:
file:py“ test1”结果
file:py“ test_1” 结果
712,000用户怎么会错?但是说真的,这是一个数量级的差异!
一些相关注释:
1
或2
,除非它们属于用户的知识域 。例如,read1
和read2
对生物学家的特定含义,他们是下一代测序分析的用户。但这很少见。即使那样,列表还是一个更好的主意(因为比您想象的要早,还会有read3
和read4
)。也许src_city
和dest_city
可以是更好的选择,city> city1
和city2
。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:
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:
1
or2
, unless they are part of the knowledge domain of the users. For example,read1
andread2
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 beread3
andread4
). Perhapssrc_city
anddest_city
can be better choices andcity1
andcity2
.