2016-01-19 29 views
3

我在下面創建了一些面向對象的Python代碼,其中創建了一個用戶實例(稱爲Ben)。這些函數正常工作,但我發現,當最後一行改爲ben.check_money = 100時,不會引發錯誤。我知道這是不正確的語法。添加正確的函數調用回然而,拋出上市的錯誤:TypeError: 'int' object is not callable奇怪的Python OOP函數調用錯誤

原始代碼:

class User: 

    def __init__(self): 
     self.money = 200 
     self.score = 0 
     print('Created ') 

    def increase_money(self, amount): 
     self.money += amount 

    def decrease_money(self, amount): 
     self.money -= amount 

    def check_money(self): 
     print(self.money) 


ben = User() 

ben.check_money() # No error is thrown here 

修改代碼1;

class User: 

    def __init__(self): 
     self.money = 200 
     self.score = 0 
     print('Created ') 

    def increase_money(self, amount): 
     self.money += amount 

    def decrease_money(self, amount): 
     self.money -= amount 

    def check_money(self): 
     print(self.money) 


**ben = User() 
ben.check_money = 100 # No error thrown here 
ben.check_money() # Error is thrown here** 

修改代碼2;

class User: 

    def __init__(self): 
     self.money = 200 
     self.score = 0 
     print('Created ') 

    def increase_money(self, amount): 
     self.money += amount 

    def decrease_money(self, amount): 
     self.money -= amount 

    def check_money(self): 
     print(self.money) 


**ben = User() 
ben.check_money = 100 # No error thrown here 
ben.check_money # No Error is thrown here** 

我的問題是;爲什麼錯誤只發生在某些情況下,取決於你以前如何調用它?人們會認爲它應該爲修改代碼1和修改代碼2輸出錯誤。

回答

3

當您這樣做時ben.check_money=100您爲ben.check_money屬性指定了一個整數100。這個屬性影響了方法ben.check_money()。現在,本只有屬性check_money。所以如果你打電話給ben.check_money,你實際上是在調用屬性,這是一個int 100ben.check_money()正在調用該方法,但該方法現在已被隱藏。

這樣做的正確的方式可能是編寫一個setter()方法:

class User: 

    def __init__(self): 
     self.money = 200 
     self.score = 0 
     print('Created ') 

    def increase_money(self, amount): 
     self.money += amount 

    def decrease_money(self, amount): 
     self.money -= amount 

    def set_money(self,amount): 
     self.money = amount 

    def check_money(self): 
     print(self.money) 


**ben = User() 
ben.set_money(100) 
ben.check_money() 
4

當你執行ben.check_money = 100你替換使用整數100ben對象上調用check_money功能。調用一個整數作爲功能不起作用:

>>> 100() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'int' object is not callable 

它設定爲100

運行後ben.check_money()時,你會得到一個類似的錯誤,但如果你簡單地訪問ben.check_money你不問Python來運行check_money,只需將它給回給您,讓您返回整數值。

2

Python是動態語言,這意味着你可以添加,刪除,或你想在任何時候你想,除非是從做採取措施,幾乎任何對象替換任何

例如

>>> class Fuu: 
    def show(self): 
     print("This is Esparta") 


>>> y=Fuu() 
>>> y.show() 
This is Esparta 
>>> y.show 
<bound method Fuu.show of <__main__.Fuu object at 0x000000000357CC50>> 
>>> y.show = 10 
>>> y.show 
10 
>>> y.a 
Traceback (most recent call last): 
    File "<pyshell#51>", line 1, in <module> 
    y.a 
AttributeError: 'Fuu' object has no attribute 'a' 
>>> y.a=42 
>>> y.a 
42 
>>> def buu(): 
    print("42 is the ultimate answer") 

>>> y.fun = buu 
>>> y.fun() 
42 is the ultimate answer 
>>>  

在這個例子中,類Fuu只定義1個屬性/方法,show,但可以在任何時間顯示在實施例等show添加額外的像afun或替換現有的。在你的代碼中,你正在做的是用一個整數代替你的原始check_money方法,而python並沒有說什麼,因爲它的本質是允許這個不像更多的靜態元素。