RE: Lazy Attribute

Discussion in 'Python' started by Andriy Kornatskyy, Nov 16, 2012.

  1. 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:
    > Date: Thu, 15 Nov 2012 15:24:40 -0700
    > Subject: Re: Lazy Attribute
    > To:
    >
    > On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy
    > <> 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
    Andriy Kornatskyy, Nov 16, 2012
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Nov 16, 2012
    #2
    1. Advertising

  3. 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, Nov 16, 2012
    #3
  4. 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.
    Rouslan Korneychuk, Nov 16, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Pu
    Replies:
    3
    Views:
    671
    Steven D'Aprano
    Jan 16, 2009
  2. Boris Borcic
    Replies:
    0
    Views:
    547
    Boris Borcic
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    545
    Boris Borcic
    Jan 16, 2009
  4. Ian Kelly

    Re: Lazy Attribute

    Ian Kelly, Nov 15, 2012, in forum: Python
    Replies:
    0
    Views:
    171
    Ian Kelly
    Nov 15, 2012
  5. Andriy Kornatskyy

    RE: Lazy Attribute

    Andriy Kornatskyy, Nov 16, 2012, in forum: Python
    Replies:
    0
    Views:
    133
    Andriy Kornatskyy
    Nov 16, 2012
Loading...

Share This Page