Created
July 17, 2013 09:46
-
-
Save lexdene/6019217 to your computer and use it in GitHub Desktop.
讨论一下Python的代码风格
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 第一个例子 | |
name = 'elephant' | |
# 第一种代码风格 | |
if 'John' == name: | |
isJohn = True | |
else: | |
isJohn = False | |
# 第二种代码风格 | |
isJohn = 'John' == name | |
# 第二个例子 | |
nameList = ['elephant', 'Liu Xiang'] | |
# 第一种代码风格 | |
if 'elephant' in nameList: | |
hasMe = True | |
else: | |
hasMe = False | |
# 第二种代码风格 | |
hasMe = 'elephant' in nameList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
两段代码,第二种风格都是找抽型的。
isJohn = 'John' == name
不容易看出 isJohn 是个 boolean,脑子要稍微停顿一下;hasMe = 'elephant' in nameList
也要停一会儿,对反应慢的来说有点跟不上思维的感觉。