Nic*_*eet 5 python optimization
我有一个带有更新其对象属性的函数的类。我试图找出哪个更Pythonic:我应该显式返回我正在更新的对象,还是简单地更新对象self?
例如:
class A(object):
def __init__(self):
self.value = 0
def explicit_value_update(self, other_value):
# Expect a lot of computation here - not simply a setter
new_value = other_value * 2
return new_value
def implicit_value_update(self, other_value):
# Expect a lot of computation here - not simply a setter
new_value = other_value * 2
self.value = new_value
# hidden `return None` statement
if __name__ == '__main__':
a = A()
a.value = a.explicit_value_update(2)
a.implicit_value_update(2)
Run Code Online (Sandbox Code Playgroud)
我环顾四周,但没有看到任何明确的答案。
编辑:具体来说,我正在寻找可读性和执行时间。对于任一功能而言,任一类别都会有优势吗?