1. 程式人生 > >關於錯誤:'str' object is not callable可能的原因之一

關於錯誤:'str' object is not callable可能的原因之一

今天寫程式碼遇到了一個問題TypeError: 'str' object is not callable。

給大家看看我的程式碼:(程式碼功能說明:用正則表示式匹配數字並對數字進行+1操作,然後替換原數字)

str='literal books=1000'
def add1(match):
    val=match.group()
    num=int(val)+1
return str(num)
info=re.sub(r'\d+',add1,str)
print info
#literal books=1001
為什麼會出錯呢?百度了一下,看到有個國外網站說“You are redefining what 
str() means. str is the built-in Python name of the string type, and you don't want to change it.Use a different name for the local variable, and remove the global statement.

翻譯過來意思是,str()是系統自帶的,你不能在用它的時候自己同時定義一個別的叫做str的變數,這樣會衝突。

於是我把自定義的str變數改成了別的名字,str1,程式碼就通過了。

這是因為如果我自定義叫str的變數,str會被系統識別成字串轉換函式,這樣的函式被傳進sub函式的引數裡面,is not callable,也就是說,是不可呼叫的。大家檢查一下自己的程式碼是不是也有類似的錯誤呢?

希望有所幫助:)

-------------------2017.3.21更新

今天學習了  __call__函式。在Python中,函式其實是一個物件,並且所有的函式都是可呼叫物件。一個類例項也可以變成一個可呼叫物件,只需要實現一個特殊方式__call__().

所以,當出現報錯 XXX is not callable的時候,很有可能是你正在呼叫一個不能被呼叫的變數或物件,具體表現就是你呼叫函式、變數的方式錯誤。

相關推薦

關於錯誤'str' object is not callable可能原因之一

今天寫程式碼遇到了一個問題TypeError: 'str' object is not callable。 給大家看看我的程式碼:(程式碼功能說明:用正則表示式匹配數字並對數字進行+1操作,然後替換原數字) str='literal books=1000' def add

錯誤: TypeError: 'str' object is not callable,TypeError: 'int' object is not callable

先說上面出現的原因: TypeError: 'str' object is not callable:就是str不可以被系統呼叫, TypeError: 'int' object is not callable:就是int不可以被系統呼叫 原因就是:你正在呼叫一個不能被

《Django學習》遇到的問題“TypeError'str' object is not callable"

今天星期六,躺在床上,準備想著自己是不是應該瞭解下python的web框架,於是,就開始鼓搗Django。 在開始第一個程式:hello,world時,剛開始挺順利,但是,在我啟動內部伺服器之後,在瀏覽器鍵入 http://127.0.0.1:8000/bl

Django錯誤'str' object is not callable

錯誤資訊: TypeError at /hello/ 'str' object is not callable Request Method: GET Request URL: http

Python中的常見報錯'xxx' object is not callable

【python】Error:’xxx’ object is not callable ‘xxx’ object is not callable,“xxx”為函式,例如int,list,str。 當出現報錯 ‘xxx’ is not callable的時候,通

常見問題記錄TypeError: 'str' object is not callable

遇到過好幾次的一個問題,今天重點記錄下! 原始碼:str = '"'+'aaa' + str(temp)+'"'+')'     sql = 'insert into students values(0,'+str 報錯:TypeError: 'str' object i

錯誤: TypeError: 'str' object is not callable,TypeError: 'int' object is not callable

先說上面出現的原因: TypeError: 'str' object is not callable:就是str不可以被系統呼叫, TypeError: 'int' object is not callable:就是int不可以被系統呼叫 原因就是:你正在呼叫一個不能被呼叫的變數或物件,

解決"學習python-flask教程,下載原始碼執行的時候出現TypeError: 'bool' object is not callable 錯誤程式碼"

 我也跟著教程敲程式碼,Google老半天,結果在一篇俄國人的帖子中找的這個,https://plus.google.com/+MiguelGrinberg/posts/9o9idDfaYhK,尼瑪,Miguel Gr

python報錯'str' object is not callable

xxx nbsp 正在 bnt 沖突 報錯 nts bsp bject   >>> x=1.235   >>> int(x)   1  >>> str="fsgavfdbafdbntsbgbt"  >>>

報錯TypeError: 'NoneType' object is not callable問題解決

報錯:TypeError: ‘NoneType’ object is not callable問題解決方法 def dec(function): print("start...") function() print("end...") #修飾器 @dec def

關於XGB.booster()報錯TypeError: 'str' object is not callable的解決方法

 當使用XGB想得到特徵重要性時報錯,程式碼及報錯如下, model = XGBRegressor( learning_rate = 0.1, n_estimators = 300, max_depth = 7, min_child_weight =

【Python】'str' object is not callable/'int' object is not callable

‘str’ object is not callable/‘int’ object is not callable Python報錯TypeError: ‘str’ object is not callable 類似的還有 ‘int’ object is not callable s

解決TypeError: 'list' object is not callable

如果list變數和list函式重名,會有什麼後果呢?我們可以參考如下程式碼: list = ['泡芙', '湯圓', '魚兒', '駱駝'] tup_1 = (1, 2, 3, 4, 5) tupToList = list(tup_1) print(tupToList)

tensorflow程式碼執行的最後丟擲異常TypeError: 'NoneType' object is not callable

tensorflow程式碼執行的最後丟擲異常:問題:Exception ignored in: <bound method BaseSession.__del__ of <tensorflo

TypeError:'dict' object is not callable

peer 錯誤 not typeerror obj dict 而不是 pytho pos TypeError:‘dict‘ object is not callable 出現這種錯誤有兩種可能: 1. 代碼裏重新定義了dict,比如 dict= {...},這時調用的

Python: TypeError: 'dict' object is not callable

color call error gpo -c typeerror python字典 type 問題 問題: TypeError: ‘dict‘ object is not callable 原因: dict()是python的一個內建函數,如果將dict自定義為一個p

python 報錯——Python TypeError: 'module' object is not callable 原因分析

erro 隱藏 import 分析 ide typeerror error: 自帶 In 原因分析:Python導入模塊的方法有兩種: import module 和 from module import 區別是前者所有導入的東西使用時需加上模塊名的限定,而後者則不需要

python3在pycharm中為什麽導入random模塊不能用? TypeError: 'module' object is not callable

新建 ron erro RR strong object bject 而且 content 新手學python求大神指導,也用sys導入了random.py的路徑,仍然不行。 剛剛排錯貌似找到了問題的原因。。。那是因為我在pycharm中新建的python文件名就是ran

AttributeError: module 'XXX' has no attribute 'xxx' && TypeError: 'module' object is not callable

AttributeError: module 'XXX' has no attribute 'xxx' && TypeError: 'module' object is not callable 最近在呼叫python的類的時候總是提示:module 'XXX' has no a

TypeError: 'bytes' object is not callable 解決方法

原始碼如下,定義的本地變數get_time和get_time()外部引入方法重名,呼叫get_time(),被當成了本地變數get_time= b'\d+ .\d+',變數不可被呼叫(函式才可呼叫)。 if __name__ == '__main__': get_time= b'\d+\