is_<whatever_you_are_testing_for> as method or property?

Chris Angelico rosuav at gmail.com
Thu Dec 11 13:20:51 EST 2014


On Fri, Dec 12, 2014 at 4:34 AM, Mateusz Loskot <mateusz at 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.

ChrisA



More information about the Python-list mailing list