Using super()

Laszlo Nagy gandalf at designaproduct.biz
Wed Jul 19 08:41:09 EDT 2006


Pupeno írta:
> Hello,
> I have a class called MyConfig, it is based on Python's
> ConfigParser.ConfigParser.
> It implements add_section(self, section), which is also implemented on
> ConfigParser.ConfigParser, which I want to call.
> So, reducing the problem to the bare minimum, the class (with a useless
> add_section that shows the problem):
>   
The problem is that ConfigParser.ConfigParser is an old style class. It 
is in the standard library, so I have no clue why. For old style 
classes, you should directly call the ancestor class method.
For new style classes it works just fine:


class ConfigParser(object):
    def add_section(self, section):
        print section, "in ",self.__class__.__name__

class MyConfig(ConfigParser):
    def add_section(self, section):
        super(MyConfig, self).add_section(section)

m = MyConfig()
m.add_section("blah")


The output:

blah in  MyConfig




More information about the Python-list mailing list