How to instantiate in a lazy way?

Slaunger Slaunger at gmail.com
Tue Dec 2 06:01:08 EST 2008


On 2 Dec., 11:30, Nick Craig-Wood <n... at craig-wood.com> wrote:

>
> For 4 attributes I'd probably go with the __getattr__.
>
OK, I'll do that!

> Or you could easily write your own decorator to cache the result...
>
> Eghttp://code.activestate.com/recipes/363602/

Cool. I never realized I could write my own decorators!
I've so far only used them for
@classmethod, @staticmethod and stuff like that.
User defined decorators are nice and fun to do as well.
I just hope it will be understandable
in four years also...

>
> >  With the property methology you do the value check on each get, which
> >  does not look as "clean". The property methology is also a little less
> >  arcane I guess for less experienced Python programmers to understand
> >  when re-reading the code.
>
> Less magic is how I would put it.  Magic is fun to write, but a pain
> to come back to.  Over the years I find I try to avoid magic more and
> more in python.
>
Ah, I see. I hope you do not consider user defined decorators
"magic" then? ;-)

> >  What kind of trouble are you referring to in __getattr__? Is it
> >  recursive calls to the method on accessing object attributes in that
> >  method itself or other complications?
>
> Every time I write a __getattr__ I get tripped up by infinite
> recursion!  It is probably just me ;-)
>
And I will probably end up having the same initial problems, but I
found an example
here, which I may try to be inspired from.

http://western-skies.blogspot.com/2008/02/complete-example-of-getattr-in-python.html

> >  On a related issue, thank you for showing me how to use @property as a
> >  decorator - I was not aware of that possibility, just gotta understand
> >  how to decorate a setter and delete method as well, but I should be
> >  able to look that up by myself...
>
> I'm sure you will!
>
> http://www.python.org/doc/2.5.2/lib/built-in-funcs.html
>
Yeah, I just visited that page yesterday!

Again, thank you for your assistance, Nick!

-- Slaunger



More information about the Python-list mailing list