2024-04-26 06:43:35 发布
网友
我经常看到人们这样写代码:
try: some_function() except: print 'something'
当我认为这样做更干净时:
我在想为什么?把代码放在与try相同的行上是否有内在的速度差异?(有点像“内衬”,但不是真的)。你知道吗
答案在于你的问题。总是使用第二种形式:它更干净。 这里还有一个不错的Python style guide你可能想看看。你知道吗
我认为前者不是一个好的做法。 人们可能认为try&except块足够短,可以写一行。你知道吗
引用PEP8:
While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!Definitely not:try: something() finally: cleanup()
While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!
Definitely not:
try: something() finally: cleanup()
(参考:http://www.python.org/dev/peps/pep-0008/)
所以,总是把try写在单独的一行是一个很好的做法。你知道吗
同意乔兰的观点,这是一个风格和可读性偏好的问题。当然,使用编码的回车符时,实际文件大小会稍大一些,但性能不会受到影响。你知道吗
答案在于你的问题。总是使用第二种形式:它更干净。 这里还有一个不错的Python style guide你可能想看看。你知道吗
我认为前者不是一个好的做法。 人们可能认为try&except块足够短,可以写一行。你知道吗
引用PEP8:
(参考:http://www.python.org/dev/peps/pep-0008/)
所以,总是把try写在单独的一行是一个很好的做法。你知道吗
同意乔兰的观点,这是一个风格和可读性偏好的问题。当然,使用编码的回车符时,实际文件大小会稍大一些,但性能不会受到影响。你知道吗
相关问题 更多 >
编程相关推荐