Skip to content Skip to sidebar Skip to footer

Python: Nested Class Inheritance

Would it be OK to declare an instance of the sub-classed class from its Base (super?) class as I do it here: if 'classB_args' in dictArg.keys() and dictArg['classB_args']: sel

Solution 1:

If you're asking whether it will work, the answer is yes, but I can't think of a situation where it is a good idea. The reason is that this locks your base class down to being really only useful with ClassA ... I think that the solution would probably be cleaner to do something like:

myDictArg = {'id':1, 'name':'MyName', 'win':'c:/windows', 'mac': '/Volumes/mac/', 'classB': ClassA(id=1, name='MyProject')}

and forgo all of the extra classB_args craziness in Base.__init__. It's more explicit so the user ends up knowing what to expect.

If you really don't like that, you could use the type of self to determine what to use rather than specifying ClassA directly which is a little better:

self['classB'] = type(self)(dictArg['classB_args'])

Now, if you derive a new class from Base, that will be used instead:

classFoo(Base): pass

instance = Foo(myDictArg)
printtype(instance.getClassB())  # Foo !

a few other non-related points.

  1. Don't do x in dct.keys() -- It's horribly inefficient in python2.x. You can just do x in dct :-).
  2. Generally speaking, writing simple wrappers like getClassB are considered "unpythonic". You can just access the data directly in python.
  3. You can rewrite the if clause to clean it up a little:

Here's how:

if'classB_args'in dictArg.keys() and dictArg['classB_args']:
        self['classB']=ClassA( dictArg['classB_args'] )

goes to:

   # with this, you can remove the `self.setdefault('classB', None)` statement.
   item = dictArg.get('classB_args')
   self['classB'] = type(self)(item) if item else item

Post a Comment for "Python: Nested Class Inheritance"