为什么上面那个不会出错,下面那个会出错
class Foo:def foo(self):
self.foo = "I love FishC.com!"
return self.foo
>>> foo = Foo()
>>> foo.foo()
'I love FishC.com!'
=======================
class Foo:
def __init__(self):
self.foo = "I love FishC.com!"
def foo(self):
return self.foo
>>> foo = Foo()
>>> foo.foo()
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
foo.foo()
TypeError: 'str' object is not callable
检查下你有没有给某个变量赋值给了str,如果有就换个变量名 foo 和 foo 同名了呀!
页:
[1]