It surprises me that this code gives the subsequent error (Python 2.7.3):
class Foo(object):
class Bar(object):
pass
class Baz(object):
class InnerBar(Bar):
pass
results in:
Enthought Python Distribution -- www.enthought.com
Version: 7.3-2 (64-bit)
Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 11 2012, 17:52:16)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "credits", "demo" or "enthought" for more information.
Hello
>>> class Foo(object):
... class Bar(object):
... pass
... class Baz(object):
... class InnerBar(Bar):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in Foo
File "<stdin>", line 5, in Baz
NameError: name 'Bar' is not defined
My suspicion is that this has to do with the particular execution of the equivalent Bar = type(...) class assignment, but even so, why doesn't Bar become available by closure within Baz?
Is there a way to workaround this for this precise kind of implementation (as in, I don't want to relax the constraint that Bar is a plain class defined inside of Foo, and that other classes within classes within Foo might want to inherit from Bar.
The particular use case that I have in mind is mimicking some database structures to make a more object oriented design that mirrors the schema in a data services project. Much of the code is autogenerated Python derived from a program that performs inspection on XSD, and so having classes defined within classes was a natural way to emulate certain things about XSD complex types, but with the added ability to also say when they are children of other complex types, etc. That's why I'm hesitant to pull a class outside of Foo for later inheritance.