On 11 December 2014 at 19:20, Chris Angelico <ros...@gmail.com> wrote: > On Fri, Dec 12, 2014 at 4:34 AM, Mateusz Loskot <mate...@loskot.net> wrote: >> If a class member function simply tests something and >> returns a b::oolean call it >> >> def is_<whatever_you_are_testing_for>(): >> pass >> >> like 'is_even'. >> >> Should I define it as a classic method >> >> def is_even(self): >> pass >> >> or as a property >> >> @property >> def is_even(self): >> pass > > A property should be used if what you're creating is "virtually an > attribute". If it would make sense to have an attribute is_even, then > a property is_even makes sense. If in doubt, go with a method.
Thanks for the advise. I've got several cases which are not obvious to me. For instance, class Foo has a boolean attribute, read-write, which I see a couple of realisations for possible: 1) properties only class Foo: @property def is_default(self): pass @is_default.setter def is_default(self, current): pass 2) property + method mix class Foo: @property def is_default(self): pass def make_default(self, current): pass 3) methods only class Foo: def is_default(self): pass def make_default(self, current): pass >From one angle, that is is_default is known and does not need to be calculated upon every query, the option 1) fits well. It is aligned with Ethan's suggestion in the other post. >From other point, let's assume updating it takes a little bit of changes of state of Foo instance, then perhaps methods-only option 3) fits best. Looks like "if in doubt, go with a method" is the safest bet. 'Statistics' from Python codebase also seem to suggest that. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net -- https://mail.python.org/mailman/listinfo/python-list