Possible Duplicate:
python - returning a default value
I have a function in a class:
def foo(self, value = None):
if value is None:
return self.value
self.value = value
return self
This is a getter and setter combined into one, with the advantage that I can chain functions like this:
three = anInstanceOfMyClass.foo(3).bar().foo()
The problem appears when I do like this:
shouldBeNoneButIsThree = anInstanceOfMyClass.foo(3).foo(None).foo()
Here foo thinks that I did not pass an argument. I can avoid this by:
- Setting value to some strange object, like a random string
- Create an object instance in the constructor which value has as default value
Both seem a bit too much work, what is an easier way?
setting value to something