inherit without calling parent class constructor?
Jeff Shannon
jeff at ccvcorp.com
Thu Jan 27 20:25:29 EST 2005
Christian Dieterich wrote:
> On Déardaoin, Ean 27, 2005, at 14:05 America/Chicago, Jeff Shannon wrote:
>
>> the descriptor approach does. In either case, the calculation happens
>> as soon as someone requests D.size ...
>
> Agreed. The calculation happens as soon as someone requests D.size. So
> far so good. Well, maybe I'm just not into it deep enough. As far as I
> can tell, In your class D the calculation happens for every
> instantiation of D, right? For my specific case, I'd like a construct
> that calculates D.size exactly once and uses the result for all
> subsequent instantiations.
Okay, so size (and the B object) is effectively a class attribute,
rather than an instance attribute. You can do this explicitly --
class D(object):
_B = None
def __getattr__(self, attr):
if self._B is None:
if myB is None:
myB = B()
D._B = myB
return getattr(self._B, attr)
Now, when the B object is first needed, it's created (triggering that
expensive calculation) and stored in D's class object. Since all
instances of D share the class object, they'll all share the same
instance of B.
Probably not worth the trouble in this particular case, but maybe in
another case... :)
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list