2

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'
2
  • Does from NewPdb import __get_except_lines(...) work? Commented Jul 17, 2013 at 19:56
  • The problem is solved now thanks to @hivert. I really appreciate everyone's help here, again, I learned something new (name mangling)! Great community! Commented Jul 17, 2013 at 20:28

3 Answers 3

2

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(...)
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Ah, okay makes sense now. The intention was that I have a parent class with this method, which (the method) should only used by myself in other methods. So I guess what I can do is just to copy&paste this method over into the child class, although it's not the "cleanest" way
@SebastianRaschka Or just not use two underscores.
I wanted to have those 2 underscores there for the "user" to know that this is one of the methods that he is not intended to use.
@SebastianRaschka The convention in such a case is to use a single underscore.
Thanks, I really didn't know that. Found a good thread here about singe and double underscore usage: stackoverflow.com/questions/6930144/…
0
super(<your_class_name>, self).<method_name>(args)

e.g.

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

2 Comments

Thanks, but now I get an AttributeError: 'super' object has no attribute '_PdbLig__get_except_lines'. I used self.cont = super(NewPdb,self).__get_except_lines(...
It's because double underscores cause name mangling. If you want to keep your variable accessible, yet giving it internal meaning, add a single underscore - this is generaly known as internally-used indicator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.