2008-07-15: Selected articles now have experimental "comment" links in the left column and at the bottom. You're welcome to use them for commenting and voting on articles. For a bit more on this, see this page. /F

How do I call a method defined in a base class from a derived class that overrides it?

If you’re using new-style classes, use the built-in super function:

class Base(object):
    def meth(self):
        pass

class Derived(Base):
    def meth(self):
        super(Derived, self).meth()

If you’re using classic classes: For a class definition such as class Derived(Base): … you can call method meth() defined in Base (or one of Base’s base classes) as Base.meth(self, arguments…). Here, Base.meth is an unbound method, so you need to provide the self argument.

class Base:
    def meth(self):
        pass

class Derived(Base):
    def meth(self):
        Base.meth(self)

CATEGORY: programming

 

A Django site. this page was rendered by a django application in 0.03s 2008-07-25 10:57:50.152720. hosted by webfaction.