Question

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'
Was it helpful?

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(...)

OTHER TIPS

super(<your_class_name>, self).<method_name>(args)

e.g.

super(PdbLig, self).__get_except_lines(...)

The entire point of putting a double underscore in front of a name is to prevent it from being called in a child class. See http://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references

If you want to do this, then don't name it with a double underscore (you can use a single underscore), or create an alias for the name on the base class (thus again defeating the purpose).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top