简体   繁体   中英

Calling private parent class method from parent class (django)

I want to call a redefined private method from an abstract parent class. I am using django if that matters.

class Parent(models.Model):
    def method1(self):
         #do somthing
         self.__method2()

    def method2(self):
         pass # I also tried calling up a prent method with super

class child(Parent):
    def method1(self)
        super(Child, self).method1()

    def __method2(self):
        #do something

I get a

AttributeError: "'Chil' object has no attribute '_Parent__method2'"

What I am doing wrong ?

Initial double underscores prevent polymorphism since both the method definition and the method call get mangled, to two different names. Replace with a single underscore to fix this.

Also, double underscores are not used for "private" attributes, and you should discard whatever reference told you that they are. They're used for MI disambiguation.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM