Created
August 19, 2013 07:22
-
-
Save lexdene/6266462 to your computer and use it in GitHub Desktop.
代码风格的讨论,关于是否返回False的问题
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
# 第一种风格 | |
def isMale(user): | |
if user['sex'] == 'm': | |
return True | |
else: | |
return False | |
# 第二种风格 | |
def isMale(user): | |
if user['sex'] == 'm': | |
return True | |
return False | |
# 第三种风格 | |
def isMale(user): | |
if user['sex'] == 'm': | |
return True | |
# 因为函数默认返回None | |
# 在Python中,if None和if False效果一样 | |
# 所以此处可以不写return |
话说还可以这样:
def is_male(user):
result = None
if user.get('sex', '') == 'm':
result = True
else:
result = False
return result
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
从Java的角度来看, 选2,另外两个根本编译不过