Skip to content

Instantly share code, notes, and snippets.

@lexdene
Created July 17, 2013 09:46
Show Gist options
  • Save lexdene/6019217 to your computer and use it in GitHub Desktop.
Save lexdene/6019217 to your computer and use it in GitHub Desktop.
讨论一下Python的代码风格
# 第一个例子
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
@woodywang
Copy link

两段代码,第二种风格都是找抽型的。isJohn = 'John' == name 不容易看出 isJohn 是个 boolean,脑子要稍微停顿一下;hasMe = 'elephant' in nameList 也要停一会儿,对反应慢的来说有点跟不上思维的感觉。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment