is_<whatever_you_are_testing_for> as method or property?

Mateusz Loskot mateusz at loskot.net
Mon Dec 15 04:56:56 EST 2014


On 13 December 2014 at 12:24, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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.

That confirms my now updated, thanks to this thread, understanding of
that issue.

Thanks all!

Best regards,
-- 
Mateusz  Loskot, http://mateusz.loskot.net



More information about the Python-list mailing list