zero argument member functions versus properties

Peter Cacioppi peter.cacioppi at gmail.com
Sun Nov 3 01:09:09 EST 2013


Python makes it very easy to turn a zero argument member function into a property (hooray!) by simply adding the @property decorator. 

(Meme for well thought py feature - "Guido was here")

But the ease with which you can do this makes the "zero argument member function or property" discussion trickier for me. 

Generally my sense here is there are two extremes

1-> the zero argument function is sort of factory-like. It potentially has non-trivial run time, or it substitutes calling a class constructor when building certain objects. 
2-> it simply retrieves a stored value (perhaps lazily evaluating it first)

so 1 should clearly be a zero argument member function. 2 should be a method.

Other than that, I say "when in doubt, go with zero argument method". In particular something in my gut says that if the thing I'm returning is itself a function, than don't go with property. In other words

foo.bar()(x) 

self documents that bar returns a function whereas 

foo.bar(x)

looks like bar is a one argument member function of foo, as opposed to a property that returns a 1 argument  function

I also think that foo.size() implies that foo performs a count every time it's called, and foo.size implies that the run time will amortize to O(1) somehow (usually with lazy eval). So the implementation should drive the property or not decision.

Sound a bit right?

Seems like some of the energetic posters will have fun with this one, re:less.



More information about the Python-list mailing list