我只想知道在python中哪种方式更可取。想象两个功能:
1个功能:
def foo(key):
if bar.has_key(key):
return bar.get(key)
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
2 功能:
def foo(key):
if bar.has_key(key):
return bar.get(key)
else:
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
如您所见,唯一的区别是else
声明。所以问题是它会以某种方式影响性能。或者有什么理由包含else
在这种类型的功能中?