Inheritance and Inner/Nested Classes

Paul Morrow pm_mon at yahoo.com
Mon Jul 12 08:04:43 EDT 2004


I'm hoping that someone can explain why I get the following exception. 
When I execute the code...

######################################
class Parent(object):
     class Foo(object):
         baz = 'hello from Parent.Foo'

class Child(Parent):
     #Foo.baz = 'hello from Child.Foo'
     pass

print Child.Foo.baz
print dir(Child)
######################################

...it displays what I expect...

hello from Parent.Foo
['Foo', '__class__', '__delattr__', '__dict__', '__doc__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__']

...but when I uncomment the first line of the Child class, Python
complains that Foo is undefined...

Traceback (most recent call last):
   File "test1.py", line 5, in ?
     class Child(Parent):
   File "test1.py", line 6, in Child
     Foo.baz = 'hello from Child.Foo'
NameError: name 'Foo' is not defined

Thanks in advance.

Paul




More information about the Python-list mailing list