MyClass = MyClass(abc)
上面一行是一樣的這樣做:
def x():
print 'Hello'
return 'Hello'
x() # outputs Hello
x = x() # Also ouputs 'Hello' (since x() is called)
x() # Error, since x is now 'Hello' (a string), and you are trying to call it.
在蟒變量的名稱僅僅是一個指針,指向存儲器中的位置。您可以通過指向其他位置將其自由分配給其他位置(對象)。事實上,你定義的大多數東西都是這樣工作的(比如方法和類)。他們只是名字。
它更奇怪,如果你的方法沒有返回值(像大多數類__init__
方法)。在這種情況下,左側是None
,您將獲得TypeError: 'NoneType' object is not callable
。像這樣:
>>> def x():
... print 'Hello'
...
>>> x()
Hello
>>> x = x()
Hello
>>> x()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> type(x)
<type 'NoneType'>
你會得到什麼異常? –