[Tutor] Calling method in parent class

The Green Tea Leaf thegreentealeaf at gmail.com
Tue May 12 10:05:00 CEST 2009


Hi,
I've started to learn Python and I'm a bit confused over how to call a
method in a parent class. Assume I have:

class Parent(object):
    def somemethod( self, bla ):
        print 'Parent',bla

I then create a child class that want to call somemethod. As I
understand it I can either do it like this

class Child(Parent):
    def somemethod( self, bla ):
        Parent.somemethod(self,bla)

or like this

class Child(Parent):
    def somemethod( self, bla ):
        super(Child,self).somemethod(bla)

The first version seem to have the obvious disadvantage that I need to
know the name of the parent class when I write the call, so I thought
that the second version was the "proper" way of doing it. But when
doing some research on the web it seem like the second version also
have some problems.

My question is simple: what is the "best" way of doing this and why?
Or should I mix both these approaches?

--
The Green Tea Leaf   thegreentealeaf at gmail.com   thegreentealeaf.blogspot.com


More information about the Tutor mailing list