Want - but cannot get - a nested class to inherit from outer class
I want - but cannot get - a nested class to inherit from an outer class. (I searched the newsgroup and the web for this, couldn't find anything - if I missed an answer to this please let me know!) I would like to build a class for a data structure such that nodes of the data structure - of interest only to the data structure implementation itself and not to the consumer - are instances of one of two class types. I thought to encapsulate the nodes' classes like this: class Tree(object): ...class _MT(Tree): ..def isEmpty(self): return True ..def insert(self, X): return Tree._Node(X) ...class _Node(Tree): ..def isEmpty(self): return False ..def insert(self, X): return _Node(X, self, Tree._MT()) ...def merge(self, T): ..def __init__(): return _MT() .. In other words, some methods would be implemented on instances' classes (like isEmpty and insert) and some on the outer class (like merge). Users of the data structure never need to know about the nodes, much less the nodes' classes, so I wanted to encapsulate them 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. What is the Pythonic thing I should be doing instead? (Easy answer: Put this code in a module, exposing only a factory function. I could do that, but wanted to know if I could encapsulate it as described so I could actually put several similar data structures into one module.) Thanks! -- David -- http://mail.python.org/mailman/listinfo/python-list
Re: Want - but cannot get - a nested class to inherit from outer class
Sorry - code for the class should read: class Tree(object): ...class _MT(Tree): ..def isEmpty(self): return True ..def insert(self, X): return Tree._Node(X) ...class _Node(Tree): ..def isEmpty(self): return False ..def insert(self, X): return _Node(X, self, Tree._MT()) ...def __init__(): return _MT() ...def merge(self, T): .. -- http://mail.python.org/mailman/listinfo/python-list
Re: Want - but cannot get - a nested class to inherit from outer class
On Mar 7, 1:19 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote: > On Fri, Mar 7, 2008 at 3:00 PM, DBak <[EMAIL PROTECTED]> 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. > > What is the Pythonic thing I should be doing instead? > > Don't nest them. The single underscore is all you need to keep any > from using them (don't forget that even if your method worked, they'd > be perfectly visible as attributes of the Tree class, or as the types > of returned values). Worrying too much about visibility is a waste of > time in Python. Yes, I knew it wouldn't stop the determined coder from finding and using them. However, I did actually have a reason for wanted them nested: I wanted to put more than one similar data structure in the same source file - and several of them would have "Empty" nodes that would behave differently. If I used nested classes all those classes could be named Empty. If flattened to module level then I'd have to invent different names for each one, like, AVL_Empty, RedBlack_Empty, and it wouldn't seem as "nice" (for some definition of "nice" that I have in my head). > > (Easy answer: Put this code in a module, exposing only a factory > > function. I could do that, but wanted to know if I could encapsulate > > it as described so I could actually put several similar data > > structures into one module.) > > There's no reason to use a factory function. If you do put them in a > module, you can use __all__ to document your exports. As with all > visibility issues in Python, this is advisory only - the only code it > affects is the symbols that are exported by "from module import *". OK. Thanks! -- David -- http://mail.python.org/mailman/listinfo/python-list
Re: Want - but cannot get - a nested class to inherit from outer class
On Mar 7, 3:41 pm, [EMAIL PROTECTED] wrote: > On Mar 7, 4:39 pm, DBak <[EMAIL PROTECTED]> wrote: > > > On Mar 7, 1:19 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote: > > > > On Fri, Mar 7, 2008 at 3:00 PM, DBak <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list