Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33415
| From | Andriy Kornatskyy <andriy.kornatskyy@live.com> |
|---|---|
| Subject | RE: Lazy Attribute |
| Date | 2012-11-16 10:49 +0300 |
| References | <CALwzidnenqoEBmzY1Xoe-BfKV-v1crRV2CdeLj1asD1ZWBDo_A@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3734.1353052215.27098.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
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
csiph-web