I have a class with a function that updates attributes of its objects. I'm trying to figure out which is more pythonic: should I explicitly return the object I'm updating, or simply update the self object?
For example:
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)
I've looked around, but haven't seen any clear answers on this.
EDIT: Specifically, I'm looking for both readability and execution time. Would there be an advantage in either category for either function?
valuecould be used by others, I'd return it. Maybe I should just always return the value, and get the best of both worlds?