Skip to content

Instantly share code, notes, and snippets.

@lexdene
Created August 19, 2013 07:22
Show Gist options
  • Save lexdene/6266462 to your computer and use it in GitHub Desktop.
Save lexdene/6266462 to your computer and use it in GitHub Desktop.
代码风格的讨论,关于是否返回False的问题
# 第一种风格
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
@xuanyu-h
Copy link

从Ruby的角度来看, 选3

@woodywang
Copy link

从Java的角度来看, 选2,另外两个根本编译不过

@seraphlnWu
Copy link

话说还可以这样:
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