【发布时间】:2014-12-20 06:17:35
【问题描述】:
我在 Python 中发现“__”函数的“奇怪”行为
class A(object):
def foo1(self):
print "foo1 A"
self.test1()
def foo2(self):
print "foo2 A"
self.__test2()
def test1(self):
print "test1 A"
def __test2(self):
print "test2 A"
class B(A):
def test1(self):
print "test1 B"
def __test2(self):
print "test2 B"
ia = A()
ib = B()
ib.foo1()
ib.foo2()
给出结果:
foo1 A
test1 B
foo2 A
test2 A
代替:
foo1 A
test1 B
foo2 A
test2 B
Python“__”函数的行为是否正常?
【问题讨论】:
-
这是不使用
__foo名称的众多充分理由之一。它们不是“私人的”,它们只是令人困惑和奇怪。