[Python-Dev] metaclass insanity

Alex Martelli aleaxit@yahoo.com
Tue, 5 Nov 2002 00:58:24 +0100


On Monday 04 November 2002 21:54, Guido van Rossum wrote:
   ...
> Can someone provide a reason why you'd want to use nested classes?
> I've never felt this need myself.  What are the motivations?

For example, I find it natural to use a nested class to provide an iterator
object for a class that defines __iter__, in many cases.  E.g. something
like:

class Outer:
    [snip snip]
    def __iter__(self):
        class Inner:
            def __init__(self, outer):
                self.outer = outer
            def __iter__(self):
                 return self
            def next(self):
                 if self.outer.isAtEnd():
                    raise StopIteration
                 else:
                    result = self.outer.currentState()
                    self.outer.advanceState()
                    return result
        return Inner(self)

this would be a typical idiom to adapt a class Outer that provides
methods isAtEnd, currentState and advanceState, with hopefully
obvious semantics, to provide an iterator object respecting Python's
iteration protocol.  Of course, I could define that "class Inner" in
any place at all, but since it's only meant to be used in this one
spot, why not define it right here?  I think it enhances legibility --
if I put it elsewhere, the reader of the code seeing just the return
statement in the def __iter__ must go look elsewhere to see what
I'm doing, and/or if the reader sees class Inner on its own it may
not be equally obvious what it's meant to be used for, while with
this placement it IS abundantly obvious.

There are other wrapping/adaptation examples that work similarly,
where I need a class just inside one particular method or function
because the only reason for that class's existence is to be suitably
instantiated to wrap another object and adapt it to some externally
imposed protocol.  I like being able to nest such "local use only"
wrapper classes in the one and only place where they're needed:
by being right there they enhance readability as outlined in the
previous paragraph, in my opinion.


Alex