Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82467 > unrolled thread

Re: is_<whatever_you_are_testing_for> as method or property?

Started byMateusz Loskot <mateusz@loskot.net>
First post2014-12-12 22:03 +0100
Last post2014-12-15 10:56 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: is_<whatever_you_are_testing_for> as method or property? Mateusz Loskot <mateusz@loskot.net> - 2014-12-12 22:03 +0100
    Re: is_<whatever_you_are_testing_for> as method or property? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-13 22:24 +1100
      Re: is_<whatever_you_are_testing_for> as method or property? Mateusz Loskot <mateusz@loskot.net> - 2014-12-15 10:56 +0100

#82467 — Re: is_<whatever_you_are_testing_for> as method or property?

FromMateusz Loskot <mateusz@loskot.net>
Date2014-12-12 22:03 +0100
SubjectRe: is_<whatever_you_are_testing_for> as method or property?
Message-ID<mailman.16927.1418418244.18130.python-list@python.org>
On 12 December 2014 at 12:26, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, Dec 12, 2014 at 10:21 PM, Mateusz Loskot <mateusz@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:
>>
>
> 0) Attribute only.
>
> class Foo:
>     pass
>
> Foo().default = True
>
> Unless you need something more than this, go with this style. Much simpler.


Agreed, in general, but if the question was related to such simple case,
I wouldn't bother asking it.

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.

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

[toc] | [next] | [standalone]


#82474

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-13 22:24 +1100
Message-ID<548c21e2$0$13005$c3e8da3$5496439d@news.astraweb.com>
In reply to#82467
Mateusz Loskot wrote:

> On 12 December 2014 at 12:26, Chris Angelico <rosuav@gmail.com> wrote:
>> On Fri, Dec 12, 2014 at 10:21 PM, Mateusz Loskot <mateusz@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

[toc] | [prev] | [next] | [standalone]


#82555

FromMateusz Loskot <mateusz@loskot.net>
Date2014-12-15 10:56 +0100
Message-ID<mailman.16978.1418637454.18130.python-list@python.org>
In reply to#82474
On 13 December 2014 at 12:24, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Mateusz Loskot wrote:
>
>> On 12 December 2014 at 12:26, Chris Angelico <rosuav@gmail.com> wrote:
>>> On Fri, Dec 12, 2014 at 10:21 PM, Mateusz Loskot <mateusz@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web