从父类调用私有父类方法(django)
我想从一个抽象的父类中调用一个重新定义的私有方法。我在使用Django,不知道这是否重要。
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
我遇到了一个
AttributeError: "'Chil' object has no attribute '_Parent__method2'"
我哪里做错了呢?
1 个回答
4
开头有两个下划线会导致多态性的问题,因为方法的定义和调用都会被改成两个不同的名字。把它们换成一个下划线就可以解决这个问题。
另外,两个下划线并不是用来表示“私有”属性的,听到这种说法就要抛掉它。它们是用来区分多重继承的。