Lazy Attribute

Andriy Kornatskyy andriy.kornatskyy at live.com
Fri Nov 16 08:49:07 CET 2012


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 at gmail.com
> Date: Thu, 15 Nov 2012 15:24:40 -0700
> Subject: Re: Lazy Attribute
> To: python-list at python.org
>
> On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy
> <andriy.kornatskyy at 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
 		 	   		  


More information about the Python-list mailing list