我知道你們大部分人都是爲那個執行而不應該使用,但是我有一些問題。'exec'不能使用私有方法Python
這裏是最小的例子,它的工作原理:
class A:
def __init__(self):
exec('self.a = self.funct()')
def funct(self):
return 1
def ret(self):
return self.a
> obj = A()
> obj.ret()
1
但是,當我這樣做:
class A:
def __init__(self):
exec('self.a = self.__funct()')
def __funct(self):
return 1
def ret(self):
return self.a
> obj = A()
AttributeError: 'A' has no attribute '__funct'
有誰知道這是爲什麼不同?
請注意'__'名稱是** class ** private。這與其他語言的隱私模型不同。 –