inherit without calling parent class constructor?
jeff at ccvcorp.com
Thu Jan 27 15:05:57 EST 2005
Christian Dieterich wrote:
> On Dé Céadaoin, Ean 26, 2005, at 17:09 America/Chicago, Jeff Shannon wrote:
>> You could try making D a container for B instead of a subclass:
> Thank you for the solution. I'll need to have a closer look at it.
> However it seems like the decision whether to do "some expensive
> calculation" or not is delegated to the exterior of class D. Different
> classes that instanciate D would need to know what has been going on
> elsewhere. That might complicate things.
True, in the sense that B is instantiated as soon as a message is sent
to D that requires B's assistance to answer. If the "decision" is a
case of "only calculate this if we actually want to use it", then this
lazy-container approach works well. If the decision requires
consideration of other factors, then it's a bit more complex -- though
one could write that logic into D, and have D throw an exception (or
return a sentinel) if it decides not to instantiate B at that time.
This then requires a bit more checking on the client side, but the
actual logic is encapsulated in D.
And really, I don't see where the container approach shifts the
decision any more than the descriptor approach does. In either case,
the calculation happens as soon as someone requests D.size ...
(However, from your other descriptions of your problem, it does sound
like a property / descriptor is a better conceptual fit than a
contained class is. I mostly wanted to point out that there are other
ways to use OO than inheritance...)
More information about the Python-list