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


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

RE: Lazy Attribute

Started byAndriy Kornatskyy <andriy.kornatskyy@live.com>
First post2012-11-16 10:49 +0300
Last post2012-11-16 13:31 +0300
Articles 6 — 3 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: Lazy Attribute Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-16 10:49 +0300
    Re: Lazy Attribute Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-16 09:04 +0000
      RE: Lazy Attribute Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-16 13:27 +0300
    Re: Lazy Attribute Rouslan Korneychuk <rouslank@msn.com> - 2012-11-16 04:32 -0500
      Re: Lazy Attribute Rouslan Korneychuk <rouslank@msn.com> - 2012-11-16 05:12 -0500
        RE: Lazy Attribute Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-16 13:31 +0300

#33415 — RE: Lazy Attribute

FromAndriy Kornatskyy <andriy.kornatskyy@live.com>
Date2012-11-16 10:49 +0300
SubjectRE: Lazy Attribute
Message-ID<mailman.3734.1353052215.27098.python-list@python.org>
Ian,

Thank you for the comments.

> The name "attribute" is not very descriptive. Why not "lazy_attribute" instead?

It just shorter and still descriptive.

> If accessing the descriptor on the class object has no special
> meaning, then the custom is to return the descriptor object itself, as
> properties do.

The lazy attribute, as a pattern, is designed to calculate something on demand, that being said means some `dynamic` nature must present, thus a class instance - object is a good candidate, while class itself is considered relatively `immutable`... of cause there might be extreme cases.

> If accessing the descriptor on the class object has no special
> meaning, then the custom is to return the descriptor object itself, as
> properties do.

If I would satisfy this, I will be forced to check for None 99.9% of the use cases (it is not None, being applied to an object). Thus it behaves as designed.

Thanks.

Andriy Kornatskyy


----------------------------------------
> From: ian.g.kelly@gmail.com
> Date: Thu, 15 Nov 2012 15:24:40 -0700
> Subject: Re: Lazy Attribute
> To: python-list@python.org
>
> On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy
> <andriy.kornatskyy@live.com> wrote:
> >
> > A lazy attribute is an attribute that is calculated on demand and only once.
> >
> > The post below shows how you can use lazy attribute in your Python class:
> >
> > http://mindref.blogspot.com/2012/11/python-lazy-attribute.html
> >
> > Comments or suggestions are welcome.
>
> The name "attribute" is not very descriptive. Why not "lazy_attribute" instead?
>
> I note that trying to access the descriptor on the class object
> results in an error:
>
> >>> from descriptors import attribute
> >>> class Foo:
> ... @attribute
> ... def forty_two(self):
> ... return 6 * 9
> ...
> >>> Foo().forty_two
> 54
> >>> Foo.forty_two
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File ".\descriptors.py", line 33, in __get__
> setattr(obj, f.__name__, val)
> AttributeError: 'NoneType' object has no attribute 'forty_two'
>
> If accessing the descriptor on the class object has no special
> meaning, then the custom is to return the descriptor object itself, as
> properties do.
>
> >>> class Foo:
> ... @property
> ... def forty_two(self):
> ... return 6 * 9
> ...
> >>> Foo().forty_two
> 54
> >>> Foo.forty_two
> <property object at 0x0280AD80>
> --
> http://mail.python.org/mailman/listinfo/python-list
 		 	   		  

[toc] | [next] | [standalone]


#33420

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-11-16 09:04 +0000
Message-ID<50a601a7$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#33415
On Fri, 16 Nov 2012 10:49:07 +0300, Andriy Kornatskyy wrote:

> Ian,
> 
> Thank you for the comments.
> 
>> The name "attribute" is not very descriptive. Why not "lazy_attribute"
>> instead?
> 
> It just shorter and still descriptive.

It is not descriptive. EVERYTHING accessed used dot notation obj.thing is 
an attribute. What makes your "attribute" different from ordinary 
attributes, properties, dynamic attributes calculated with __getattr__, 
methods, etc?



-- 
Steven

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


#33424

FromAndriy Kornatskyy <andriy.kornatskyy@live.com>
Date2012-11-16 13:27 +0300
Message-ID<mailman.3742.1353061695.27098.python-list@python.org>
In reply to#33420
Same applies to properties... they are seen as an object attributes.

Thanks.

Andriy


----------------------------------------
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Lazy Attribute
> Date: Fri, 16 Nov 2012 09:04:39 +0000
> To: python-list@python.org
>
> On Fri, 16 Nov 2012 10:49:07 +0300, Andriy Kornatskyy wrote:
>
> > Ian,
> >
> > Thank you for the comments.
> >
> >> The name "attribute" is not very descriptive. Why not "lazy_attribute"
> >> instead?
> >
> > It just shorter and still descriptive.
>
> It is not descriptive. EVERYTHING accessed used dot notation obj.thing is
> an attribute. What makes your "attribute" different from ordinary
> attributes, properties, dynamic attributes calculated with __getattr__,
> methods, etc?
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
 		 	   		  

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


#33421

FromRouslan Korneychuk <rouslank@msn.com>
Date2012-11-16 04:32 -0500
Message-ID<tKnps.2$tm5.0@newsfe08.iad>
In reply to#33415
On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote:
>> If accessing the descriptor on the class object has no special
>> meaning, then the custom is to return the descriptor object itself, as
>> properties do.
>
> If I would satisfy this, I will be forced to check for None 99.9% of the use cases (it is not None, being applied to an object). Thus it behaves as designed.

That's not true. You can use a try-except block to return the descriptor 
object when an AttributeError is raised.

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


#33422

FromRouslan Korneychuk <rouslank@msn.com>
Date2012-11-16 05:12 -0500
Message-ID<%jops.44$TT7.13@newsfe04.iad>
In reply to#33421
On 11/16/2012 04:32 AM, Rouslan Korneychuk wrote:
> On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote:
>>> If accessing the descriptor on the class object has no special
>>> meaning, then the custom is to return the descriptor object itself, as
>>> properties do.
>>
>> If I would satisfy this, I will be forced to check for None 99.9% of
>> the use cases (it is not None, being applied to an object). Thus it
>> behaves as designed.
>
> That's not true. You can use a try-except block to return the descriptor
> object when an AttributeError is raised.

Actually, never mind. I just realized the function has to be called 
before the attribute can be set, which can not-only raise any exception, 
but could potentially have undesired side-effects given a parameter it 
doesn't expect.

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


#33427

FromAndriy Kornatskyy <andriy.kornatskyy@live.com>
Date2012-11-16 13:31 +0300
Message-ID<mailman.3743.1353061987.27098.python-list@python.org>
In reply to#33422
This is very minor use case. Unlikely useful to add any checks for None, or translate one exception to the other... with pretty much the same outcome: it makes sense in objects only.

Thanks.

Andriy

----------------------------------------
> From: rouslank@msn.com
> Subject: Re: Lazy Attribute
> Date: Fri, 16 Nov 2012 05:12:11 -0500
> To: python-list@python.org
>
> On 11/16/2012 04:32 AM, Rouslan Korneychuk wrote:
> > On 11/16/2012 02:49 AM, Andriy Kornatskyy wrote:
> >>> If accessing the descriptor on the class object has no special
> >>> meaning, then the custom is to return the descriptor object itself, as
> >>> properties do.
> >>
> >> If I would satisfy this, I will be forced to check for None 99.9% of
> >> the use cases (it is not None, being applied to an object). Thus it
> >> behaves as designed.
> >
> > That's not true. You can use a try-except block to return the descriptor
> > object when an AttributeError is raised.
>
> Actually, never mind. I just realized the function has to be called
> before the attribute can be set, which can not-only raise any exception,
> but could potentially have undesired side-effects given a parameter it
> doesn't expect.
> --
> http://mail.python.org/mailman/listinfo/python-list
 		 	   		  

[toc] | [prev] | [standalone]


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


csiph-web