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



On Mar 7, 3:41 pm, castiro...@xxxxxxxxx wrote:
On Mar 7, 4:39 pm, DBak <david...@xxxxxxxxx> wrote:

On Mar 7, 1:19 pm, "Chris Mellon" <arka...@xxxxxxxxx> wrote:

On Fri, Mar 7, 2008 at 3:00 PM, DBak <david...@xxxxxxxxx> 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
.



Relevant Pages

  • Re: Need a way for forgetful developers
    ... Almost every developers define classes and use them in anothers. ... If A is a class and B uses A then we can name B as outer class and A as inner class. ... In its initialize method, outer class should call the initialize method of every inner class objects, ...
    (microsoft.public.vc.language)
  • Re: Trying to create a working internal confirm JOptionPane
    ... inner class, because it is an inner class, belongs to an object of the outer ... That means there is an outer class "this", ... It is not a static variable reference, but a completely different part of the Java language. ... Your mistake was thinking that one part of the Java language should follow rules from another part. ...
    (comp.lang.java.help)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >> outer class from the inner class, I get a compile error that says I can't ... > You need to clarify what you mean by 'in the outer class'. ... > make a long story short, an anonymous inner class can only access local ... Okay, let me rephrase, to make sure I understand this correctly. ...
    (comp.lang.java.gui)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >>You need to clarify what you mean by 'in the outer class'. ... >>An anymous inner class relies on some compiler magic. ... >>copies the values of all local variables into corresponding variables of ... an anonymous inner class can only access local ...
    (comp.lang.java.gui)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >anonymous inner class cannot access variables from the outer class unless ... >have no problem accessing the variables in the outer class. ... Finalize is not the same as final. ... have local variables. ...
    (comp.lang.java.gui)