Static Nested Classes in Python and Jython

After experimenting with static nested classes in JavaScript, I wondered whether this was possible in Python or Jython.

It seems to work perfectly in both.

I’ve only ever read two Python books… the O’Reilly Learning Python 2nd Edition and the O’Reilly Jython Essentials book… I’ve also skimmed the O’Reilly Python in a Nutshell book. None of those mentioned the ability to create nested member classes.

Seems strange to find such an interesting language feature like this. Can anyone provide links to any documentation or examples that describe this feature more fully?

Running the following through the Python 2.4.1, Python 2.3.5 (What’s up with Mac OS X Tiger shipping with Python 2.3?) and Jython 2.1 interpreter on my PowerBook:


class Outer:

    def __init__(self):
        print 'hello from Outer constructor'

    class Inner:

        def __init__(self):
            print 'hello from Inner constructor'

obj = Outer.Inner()

Produced this result as hoped:


hello from Inner constructor


About this entry