Want - but cannot get - a nested class to inherit from outer class

DBak davidbak at gmail.com
Fri Mar 7 20:46:54 EST 2008


On Mar 7, 3:41 pm, castiro... at gmail.com wrote:
> On Mar 7, 4:39 pm, DBak <david... at gmail.com> wrote:
>
> > On Mar 7, 1:19 pm, "Chris Mellon" <arka... at gmail.com> wrote:
>
> > > On Fri, Mar 7, 2008 at 3:00 PM, DBak <david... at gmail.com> wrote:
> > > >  However I can't do this, because, of course, the name Tree isn't
> > > >  available at the time that the classes _MT and _Node are defined, so
> > > >  _MT and _Node can't inherit from Tree.
>
> > > Not only is the name not defined, the class doesn't even exist yet.
>
> > Yes, but, well - it certainly isn't usable yet, but some object (that
> > will be the class when it is finished) is being built (its __dict__ is
> > being populated, etc.) - so there's an object pointer available inside
> > the interpreter that could be put somewhere.  But this is pedantic -
> > you're right, the class really isn't available until after the class
> > statement.
>
> There is no obvious solution-- What do you mean?  If there are any at
> all, there is significant competition without clear winners.
>
> dict dictA:
>    membA= 0
>    membB= 0
>
> dict dictB:
>    membC= 0
>
> But, if you try to nest them, do you want the rest of the 'dict' at
> its outer level evaluated (that's your 'here is the crux'), or only
> that point so far?
>
> dict dictO:
>   membA= 0
>   dict membB:
>     membC= 0
>     membD= membE
>   membE= 0
>
> So, you can't refer to it at all.  Especially if membE is defined in
> outer scope.

Thanks for your answer.  To the extent I understand it:  There is a
difference between the class statements I was trying to nest, with the
inner inheriting from the outer, and your dict example.  The main
thing is that in the class example - when the inner class is being
built (i.e., inside its class statement's block) there is no need (as
I understand it) for the parent class to be functional at all WHILE I
AM DEFINING METHODS on the inner class.  Sure, if the inner class had
code to be executed immediately, such as statements setting class
attributes on the inner class, and that code used names such that
attribute lookup needed to be done, then that might or might not work
- it would depend on where the names were defined in the outer class
relative to the placement of the inner class statement - exactly like
the fact that the order of definition matters when executing code when
defining a module: functions defined in a module can, in their body,
name functions not yet defined, but assignment statements to module
attributes cannot (they get a run time error).

But in the case I'm talking about I just want to define methods on the
inner class, using names such that when the method is eventually
called on an instance of the inner class the attribute lookup will
proceed with the outer class being the inner class' parent.  Creating
instances of the inner class won't happen until after the inner class
and the outer class are both fully created (and assigned to their
names) so any name lookup using inheritance won't happen until both
class objects are fully created, so ... if you could do it ... it
would work fine.

Anyway, I know it can't be done the way I wanted it - the attribute
with the outer class' name hasn't been assigned yet when I need to
reference it in the inner class' class statement - so I was just
looking to see what the preferred alternative was.  Based on the
discussion so far it seems I should just forget about using nested
classes and flatten everything to the module level, using the __all__
attribute to make it clear to the user of the data structure what
pieces of the module he should actually be using.

-- David



More information about the Python-list mailing list