Inheritance and Inner/Nested Classes

Paul Morrow pm_mon at yahoo.com
Mon Jul 12 16:28:08 CEST 2004


Peter Hansen wrote:

> Sorry, let me try the question in a different way.  What end
> goal, without reference to *how* you think you want to achieve it,
> are you trying to achieve?  You've posted what are obviously
> contrived examples.  I can't see the purpose here... other
> than "different internal states", but if that's all it is
> you don't need to have an inner class to do it (as you show
> you know by the following:)

Actually, it's the *how* that is my end goal <grin>.

I'm creating a framework that you parameterize by supplying objects that 
contain particularly named attributes.  The attributes tell the 
framework what to do, when to do it, etc.  So I'm trying to find a nice 
and clean object structure that uses a minimum of syntax.

The framework parameters (in the object the developer supplies) fall 
into a number of categories, so it seemed like a good idea to group 
them, hence the inner classes idea.  And yes, since we are talking about 
objects, a more traditional object-oriented layout such as...

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

class Child(Parent):
     class Foo(Parent.Foo):
         def __init__(self):
             self.baz = 'hello from Child.Foo'

x1 = Parent.Foo()
x2 = Child.Foo()
print x1.baz
print x2.baz
#####################################

...would work, but I was hoping to avoid having to create the 
constructor methods as well as (re)declare the inner class in the 
descendent/child classes.  It looks like class attributes take care of 
the former...

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

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

x1 = Parent.Foo()
x2 = Child.Foo()
print x1.baz
print x2.baz
#####################################

... but I don't see any way to avoid the later.

I know that this is still a contrived example, but just imagine that the 
  Foo inner class represents some customization category, and baz is a 
customization variable in that category.

Thanks again.




More information about the Python-list mailing list