inherit without calling parent class constructor?
Christian Dieterich
cdieterich at geosci.uchicago.edu
Wed Jan 26 13:59:10 EST 2005
On Dé Céadaoin, Ean 26, 2005, at 11:46 America/Chicago, Steven Bethard
wrote:
> I'm confused as to how you can tell when it's avoidable... Do you
> mean you don't want to call 'method' if you don't have to? Could you
> make size a property, e.g.
> Then 'size' won't be calculated until you actually use it. If 'size'
> is only to be calculated once, you might also look at Scott David
> Daniels's lazy property recipe:
On Dé Céadaoin, Ean 26, 2005, at 12:03 America/Chicago, Daniel Dittmar
wrote:
> - rename B to A
> - class B (A)
> - move the costly constructor from A to B
> - class D (A)
>
> You can now move some parts from B.__init__ to A.__init__ if they are
> really needed by D as well.
Thanks for the input.
Yes, I see. I should have been more specific about 'when it's
avoidable'. I guess this was part of my question: Is it avoidable?
Steven, your solution works fine, as long as I don't access the size
attribute. However,
my instances of A use their size attributes quite often within other
methods of A (not listed in my example). So, I'd just postpone the
execution of the expensive part until later.
Daniel, maybe I should have made it clearer in my example. The
"A"-objects actually need the size attribute. If I'd outsource the size
attribute (and its computation) to a class I don't inherit from, the
"A"-objects won't have access to it. Or did I miss something in your
solution?
The size attribute only needs to be computed once and stays constant
after that. The lazy property recipe of Scott David Daniels looks
promising. I'll try that, when I've installed Python 2.4. However, I
need my package to work on machines where there is Python 2.2 and 2.3
only.
Thanks for more ideas,
Christian
More information about the Python-list
mailing list