is_<whatever_you_are_testing_for> as method or property?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Dec 13 06:24:17 EST 2014


Mateusz Loskot wrote:

> On 12 December 2014 at 12:26, Chris Angelico <rosuav at gmail.com> wrote:
>> On Fri, Dec 12, 2014 at 10:21 PM, Mateusz Loskot <mateusz at loskot.net>
>> wrote:
>>> 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:
[...]
> I mentioned, setting the new value involves more changes to Foo()
> instance, so i's not possible to capture it with just an assignment.
> Hence, the discussion between property vs method.

If the calculation is cheap and fast and "feels like" getting/setting an
attribute, then use property.

If the calculation is expensive, or might fail, then use explicit
getter/setter methods.

I'm afraid that there is no objective way to tell when something "feels
like" an attribute. That depends partly on the class you are dealing with,
and partly on personal taste.



-- 
Steven




More information about the Python-list mailing list