Late initialization using __getattribute__

Bruno Desthuilliers bdesth.quelquechose at
Thu Sep 4 19:57:01 CEST 2008

bukzor a écrit :
>>>>> so unfortunately I think I need to use __getattribute__
>>>>> to do this. I'm doing all this just to make the connection not
>>>>> actually connect until used.
>>>> I may be dumb, but I don't get how this is supposed to solve your
>>>> problem. But anyway : there's a known design pattern for what you're
>>>> trying to do, that doesn't require mixins nor messing with
>>>> __getattribute__ (which, I repeat, is more often than not something you
>>>> *don't* want to do). The name of the design pattern is "proxy". I
>>>> strongly suggest that you 1/ try to cure the real problem instead of
>>>> hacking around and 2/ read about the proxy design pattern.
>>>> My 2 cents...
>>> I like the idea of mix-ins, but can't figure out how to make a proxy
>>> work that way.
>> You mean, "how to use a proxy for lazy initialization" ? Heck, that's
>> the exact use case in the GoF.
> I mean, "how to make a MixIn class that uses the proxy pattern". 

That's not how proxies work. They use composition/delegation, not MI.

> like to be able to do something like this:
> class SuperCursor(FeatureOneMixIn, FeatureTwoMixin, ...,
> VanillaCursor): pass


Sorry, but it reminds me of the worst MI abuses in Zope2.


> My toy example turned out to be not the best representation of the
> problem.

This is often the case with toy examples.

> The base class has attributes that "exist" but either throw errors or
> segfault
> if used before reconnect() is called.

Then these attributes should probably call reconnect() by themselves. 
Decorators and/or computed attributes may help here.

More information about the Python-list mailing list