1

I have a long, but simple linear hierarchy. My Grandparent class needs to provide a common _something method for all children (however distant), but the thing is it needs to call a NextClass when it's done.

In this case, the NextClass is always the parent.

Is it considered alright to do this:

class Grandparent(object):
    def __init__(self):
        pass

    def _something(self, NextClass):
        print "I did something."
        return NextClass()

class Parent(Grandparent):
    def __init__(self):
        print 'Parent called.'
        super(Parent, self).__init__()

class Child(Parent):
    def __init__(self):
        super(Child, self).__init__()

    def _special_something(self):
        self._something(self.__bases__[0])


'Parent called.'
'I did something.'
'Parent called.'

How do I better convey what is going on here?

def _special_something(self):
    parent_class = self.__bases__[0]
    self._something(parent_class)

Which is explicit, but it looks like an instance. This breaks naming convention, but I think is clearer.

def _special_something(self):
    ParentClass = self.__bases__[0]
    self._something(ParentClass)

"Variables" aren't supposed to be camel-cased, but is this a variable? When everything is an object, it gets hard to tell where the rules apply.

yurisich
  • 6,991
  • 7
  • 42
  • 63

0 Answers0