Python风格指南 [英] Python style guidelines
问题描述
有没有比PEP 8更新的Python风格指南,
Python Code的样式指南,由van Rossum和华沙,
http://www.python.org/peps/pep-0008.html ,日期是7月5日,
2001?
beliavsky>是否有更新的Python样式指南而不是
beliavsky> PEP 8,Python Code的样式指南,作者van Rossum和
beliavsky>华沙,在 http://www.python.org/peps/pep -0008.html ,
beliavsky>是2001年7月5日的日期?
你认为应该有什么缺失吗?因为PEP需要不断更新,所以没有特别的原因。特别是,好的Python风格的概念在过去的十年中没有发生太大的变化。
Skip
>你认为应该有什么遗失吗?没有特别的因为PEP需要不断更新。特别是,好的Python风格的概念在过去十年中没有发生太大的变化。
Skip
我同意,那些是很好的指导方针,但我不同意:
- 不要使用==将布尔值与True或False进行比较(bool
类型是Python 2.3中的新功能:
否:如果问候==真:
是:如果问候:
如果你这样做会发生什么:
a =''test''
if a.find(''foo''):
.... print" foo was found
....
foo被发现
我认为你永远不应该进行直接的布尔比较。相反,
应该使用更精细的布尔表达式,如:
a =''test''
如果a.find(''foo'')> = 0:
.... print" foo was found
....
如果您来自语言,那么您将特别遇到问题
小于零的值被认为是假的。我认为
以前的sintax是模棱两可的。
问候,
Josef
Josef Meile写道:
你认为应该有什么遗漏吗?没有什么特别的原因PEP需要不断更新。特别是,好的Python风格的概念在过去的十年中没有发生太大的变化。
Skip
我同意,这些都是很好的指导方针,但我不同意:
- 不要使用==(bool
类型是Python 2.3中的新类型)将布尔值与True或False进行比较:
否:如果问候==真:
是的:如果问候:
如果你这样做会发生什么:
>>> a =''test''
>>>如果a.find(''foo''):...打印foo被发现
...
foo被发现
我想你永远不应该做直接布尔比较。相反,应该使用更精细的布尔表达式,例如:
>>> a =''test''
>>>如果a.find(''foo'')> = 0:
...打印foo被发现
...
如果您来自语言中的小于零的值被认为是假的,那么您将特别遇到问题。我认为
以前的sintax是模棱两可的。
我认为你的find()示例与
$ b无关$ b如果boolVal ==真:
对比
如果boolVal:
虽然它对于来自基于1的
指数的语言的人来说可能是一个陷阱。如果您放弃它返回的索引,您可以使用
清除器
如果foo在一个:
而不是。
至于最初的问题,我不同意你的意见并且更喜欢推荐的
风格。即使对于非布尔人我也觉得很自然:
greeting ="嗨
如果问候:
打印问候语
如果你期望一个布尔值,并担心你可能会错误地遇到
别的东西,那么值== True测试就不够了,
因为治疗,比方说,嗨因为False可能也是一个错误。这是
如果问候==真:
...
elif greeting == False:
通过
否则:
引发AssertionError,问候语必须为真或假
对
断言问候语(True,False),问候语必须为真或假
如果问候:
.. 。
然后,最后一次在我眼中获胜。
彼得
Is there a more recent set of Python style guidelines than PEP 8,
"Style Guide for Python Code", by van Rossum and Warsaw, at
http://www.python.org/peps/pep-0008.html , which is dated July 5,
2001?
beliavsky> Is there a more recent set of Python style guidelines than
beliavsky> PEP 8, "Style Guide for Python Code", by van Rossum and
beliavsky> Warsaw, at http://www.python.org/peps/pep-0008.html , which
beliavsky> is dated July 5, 2001?
Is there something missing you think should be there? There''s no particular
reason a PEP needs to be continually updated. In particular, notions of
good Python style haven''t changed a lot over the past ten years.
Skip
> Is there something missing you think should be there? There''s no particularreason a PEP needs to be continually updated. In particular, notions of
good Python style haven''t changed a lot over the past ten years.
Skip
I agree, those are good guidelines, but I don''t agree with:
- Don''t compare boolean values to True or False using == (bool
types are new in Python 2.3):
No: if greeting == True:
Yes: if greeting:
What would happened if you do:
a=''test''
if a.find(''foo''): .... print "foo was found"
....
foo was found
I think you should never do a direct boolean comparison. Instead
one should use a more elaborate boolean expresion like:
a=''test''
if a.find(''foo'') >= 0:
.... print "foo was found"
....
You will have problems specially if you come from languages where
values minor than zero are considered to be false. I think the
previous sintax is ambiguous.
Regards,
Josef
Josef Meile wrote:
Is there something missing you think should be there? There''s no
particular
reason a PEP needs to be continually updated. In particular, notions of
good Python style haven''t changed a lot over the past ten years.
Skip
I agree, those are good guidelines, but I don''t agree with:
- Don''t compare boolean values to True or False using == (bool
types are new in Python 2.3):
No: if greeting == True:
Yes: if greeting:
What would happened if you do:>>> a=''test''
>>> if a.find(''foo''): ... print "foo was found"
...
foo was found
I think you should never do a direct boolean comparison. Instead
one should use a more elaborate boolean expresion like:
>>> a=''test''
>>> if a.find(''foo'') >= 0:
... print "foo was found"
...
You will have problems specially if you come from languages where
values minor than zero are considered to be false. I think the
previous sintax is ambiguous.
I think your find() example has nothing to do with
if boolVal == True:
versus
if boolVal:
though it might be a pitfall for people coming from languages with 1-based
indices. If you discard the index it returns anyway, you could use the
clearer
if "foo" in a:
instead.
As to the original issue, I disagree with you and prefer the recommended
style. I find it natural even for non-booleans:
greeting = "Hi"
if greeting:
print greeting
If you expect a boolean value and fear that you may erroneously encounter
something else, the value == True test will not be sufficient anyway,
because treating, say, "Hi" as False may be an error, too. It''s
if greeting == True:
...
elif greeting == False:
pass
else:
raise AssertionError, "greeting must be True or False"
versus
assert greeting in (True, False), "greeting must be True or False"
if greeting:
...
then, and again the last wins at in my eyes.
Peter
这篇关于Python风格指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!