我非常喜欢遵循PEP 8中指定的样式标准.我有一个自动检查它的linter,因此我的代码肯定更好.
在PEP 8中只有一点,E251和E221感觉不太好.来自JavaScript背景,我曾经将变量赋值对齐如下:
var var1 = 1234;
var2 = 54;
longer_name = 'hi';
var lol = {
'that' : 65,
'those' : 87,
'other_thing' : true
};
Run Code Online (Sandbox Code Playgroud)
在我看来,这大大提高了可读性.问题是,PEP 8不推荐这样做.对于词典,并不是那么糟糕,因为在冒号后允许空格:
dictionary = {
'something': 98,
'some_other_thing': False
}
Run Code Online (Sandbox Code Playgroud)
我可以在没有对齐的情况下"生活"变量赋值,但我完全不喜欢的是不能在函数调用中传递命名参数,如下所示:
some_func(length= 40,
weight= 900,
lol= 'troll',
useless_var= True,
intelligence=None)
Run Code Online (Sandbox Code Playgroud)
所以,我最终做的是使用字典,如下所示:
specs = {
'length': 40,
'weight': 900,
'lol': 'troll',
'useless_var': True,
'intelligence': None
}
some_func(**specs)
Run Code Online (Sandbox Code Playgroud)
或者只是简单地
some_func(**{'length': 40,
'weight': 900,
'lol': 'troll',
'useless_var': True,
'intelligence': None})
Run Code Online (Sandbox Code Playgroud)
但我觉得这个工作比忽略PEP 8 E251/E221更糟糕.
什么是最佳做法?
多年后编辑 …
我有这个代码:
some_list = range(a, b+1)
Run Code Online (Sandbox Code Playgroud)
用vim的pep8插件检查我的编码风格后,我得到了这个警告:
missing whitespace around operator
Run Code Online (Sandbox Code Playgroud)
似乎要符合PEP 8我应该写这个吗?
some_list = range(a, b + 1)
Run Code Online (Sandbox Code Playgroud)
但我已多次阅读PEP 8 - Python代码样式指南,但无法找到适用于上述警告的规则.
所以我想知道:当使用PEP-8样式时,在函数的参数中是否需要围绕运算符(+, - ,*,/等)的空格?
请考虑以下代码:
def add_function(a, b):
c = str(a) + b
print "c is %s" % c
def add_int_function(c, d):
e = c + d
print "the vaule of e is %d" % e
if __name__ =="__main__":
add_function(59906, 'kugrt5')
add_int_function(1, 2)
Run Code Online (Sandbox Code Playgroud)
它总是告诉我:"预计2个空行,在1中找到1" add_int_function,但不在add_function.
当我在前面加两个空格def add_int_function(c, d):
存在错误显示unindent does not match any outer indentation level
在结束add_function:
目前推荐的用“and”和“or”运算符打破一长串if语句的推荐方法是什么?
第一个选项
使用下面的样式(来自 PEP8)和 flake8 我收到警告:二元运算符后的 W504 换行符:
if (this_is_one_thing and
that_is_another_thing):
do_something()
Run Code Online (Sandbox Code Playgroud)
第二个选项
if (this_is_one_thing
and that_is_another_thing):
do_something()
Run Code Online (Sandbox Code Playgroud)
现在我在二元运算符之前收到警告 W503 换行符。第二个似乎符合PEP8 的这个建议
我试图找到答案,但我仍然不确定。我认为也许使用第二个选项并禁用 W503 警告将是处理此问题的一种方法?
以下哪个if语句更像Pythonic?
if not a and not b:
do_something
Run Code Online (Sandbox Code Playgroud)
要么
if not ( a or b ):
do something
Run Code Online (Sandbox Code Playgroud)
它不是谓词逻辑所以我应该使用Python关键词,因为它更具可读性吗?
在后面的解决方案比另一个更优化?(我不相信.)
这有什么PEP-8指南吗?
这两种方法的字节代码(如果重要):
In [43]: def func1():
if not a and not b:
return
....:
....:
In [46]: def func2():
if not(a or b):
return
....:
....:
In [49]: dis.dis(func1)
2 0 LOAD_GLOBAL 0 (a)
3 UNARY_NOT
4 JUMP_IF_FALSE 13 (to 20)
7 POP_TOP
8 LOAD_GLOBAL 1 (b)
11 UNARY_NOT
12 JUMP_IF_FALSE 5 (to 20)
15 POP_TOP
3 16 LOAD_CONST 0 …Run Code Online (Sandbox Code Playgroud) 我找到了pep8的文档但是无法理解如何编写这些文档.我甚至找不到任何除了设置max-line-length和ignore之外的选项的例子.
我正在尝试编写一个.pep8.rc文件,其中包括我需要执行以下操作:
./random)有人可以回答一个例子或链接到一个?
在python中,我们在开头用下划线指定内部函数/私有方法.是否应该使用docstrings记录这些功能(是否需要?)?(我的意思是正式文档,而不是帮助代码阅读器理解代码的文档)这是什么常见的做法?
为了检查python模式中的代码,我使用flymake和pyflakes
另外我想用pylint检查代码样式(pep8)(与pyflakes在同一页面上的描述)
此解决方案有效.但我无法配置flymake与pyflakes和pylint一起工作.我该怎么做?
PEP8信息:
models.py:10:80: E501 line too long (83 > 79 characters)
Run Code Online (Sandbox Code Playgroud)
Models.py:
field = TreeForeignKey('self', null=True, blank=True, related_name='abcdefgh')
Run Code Online (Sandbox Code Playgroud)
如何正确写这条线?
以下是PEP8中描述函数名称应如何的部分:
函数名称应为小写,并根据需要用下划线分隔,以提高可读性.
只有在已经成为流行风格的情境中才允许使用mixedCase
他们为什么不改变功能名称?这对于不保持向后兼容性的Python 3尤其重要.
pep8 ×10
python ×9
coding-style ×2
django ×1
emacs ×1
flake8 ×1
pep ×1
pycharm ×1
pyflakes ×1
pylint ×1
python-2.7 ×1
python-3.x ×1
standards ×1
unittest2 ×1