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'
  • Does from NewPdb import __get_except_lines(...) work? – debianplebian Jul 17 '13 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! –  Jul 17 '13 at 20:28

3 Answers3

1

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(...)
hivert
  • 10,579
  • 3
  • 31
  • 56
0
super(<your_class_name>, self).<method_name>(args)

e.g.

super(PdbLig, self).__get_except_lines(...)
Maciej Gol
  • 15,394
  • 4
  • 33
  • 51
  • 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(...` –  Jul 17 '13 at 20:20
  • It's because double underscores cause [name mangling](http://docs.python.org/release/1.5/tut/node67.html). 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. – Maciej Gol Jul 17 '13 at 20:27
0

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

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • 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 –  Jul 17 '13 at 20:22
  • @SebastianRaschka Or just not use two underscores. – Marcin Jul 17 '13 at 20:24
  • 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. –  Jul 17 '13 at 20:42
  • 1
    @SebastianRaschka The convention in such a case is to use a single underscore. – Marcin Jul 17 '13 at 20:49
  • Thanks, I really didn't know that. Found a good thread here about singe and double underscore usage: http://stackoverflow.com/questions/6930144/underscore-vs-double-underscore-with-variables-and-methods –  Jul 17 '13 at 21:32