0
我想与私有变量一起工作,所以做了这个测试用例。python,__str__一个私有变量
class X():
def __init__(self):
self.__a = 0
def __str__(self):
print(self.getA())
def getA(self):
return self.__a
x = X()
print(x.getA())
print(str(x.getA()))
print(x)
输出为:
0
0
0
Traceback (most recent call last):
File "/Users/lego90511/Documents/workspace/dummy/dummy.py", line 150, in <module>
print(x)
TypeError: __str__ returned non-string (type NoneType)
是错误,因为它是私有的?因为这对我来说没有意义,因为getA()有效。
好吧我修正了str行,所以它现在str(self.getA()),但我仍然得到相同的错误。 – EasilyBaffled 2013-03-24 19:39:53
'return str(self.getA())'或'return str(self .__ a)' – 2013-03-24 20:44:33