Search code examples
pythonclassinheritancemethodsattributeerror

Calling a method from a parent class in Python


Can anyone help me with the correct syntax to call my method __get_except_lines(...) from the parent class?

I have a class with a method as shown below. This particular method has the 2 underscores because I don't want the "user" to use it.

NewPdb(object)
    myvar = ...
    ...
    def __init__(self):
        ...
    def __get_except_lines(self,...):
        ...

In a separate file I have another class that inherits from this class.

from new_pdb import NewPdb

    PdbLig(NewPdb):
        def __init__(self):
            ....
            self.cont = NewPdb.myvar
            self.cont2 = NewPdb.__get_except_lines(...)

And I get an attribute error that really confuses me:

AttributeError: type object 'NewPdb' has no attribute '_PdbLig__get_except_lines'

Solution

  • Your problem is due to Python name mangling for private variable (http://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references). You should write:

    NewPdb._NewPdb__get_except_lines(...)