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'
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(...)
super(<your_class_name>, self).<method_name>(args)
例如
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.