Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: Lazy Attribute (http://www.velocityreviews.com/forums/t954546-re-lazy-attribute.html)

Andriy Kornatskyy 11-16-2012 07:49 AM

RE: Lazy Attribute
 

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 ondemand, that being said means some `dynamic` nature must present, thusa 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/...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


Steven D'Aprano 11-16-2012 09:04 AM

Re: Lazy Attribute
 
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

Rouslan Korneychuk 11-16-2012 09:32 AM

Re: Lazy Attribute
 
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.

Rouslan Korneychuk 11-16-2012 10:12 AM

Re: Lazy Attribute
 
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.


All times are GMT. The time now is 07:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57