欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中str is not callable問題詳解及解決辦法

 更新時(shí)間:2017年02月10日 11:43:45   投稿:lqh  
這篇文章主要介紹了Python中str is not callable問題詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下

Python中str is not callable問題詳解及解決辦法

問題提出:

   在Python的代碼,在運(yùn)行過程中,碰到了一個(gè)錯(cuò)誤信息:

   python代碼:

def check_province_code(province, country): 
  num = len(province) 
   
  while num <3: 
    province = ''.join([str(0),province]) 
    num = num +1 
   
  return country + province 

  運(yùn)行的錯(cuò)誤信息:

check_province_code('ab', '001') 
--------------------------------------------------------------------------- 
TypeError                 Traceback (most recent call last) 
<ipython-input-44-02ec8a351cce> in <module>() 
----> 1 check_province_code('ab', '001') 
 
<ipython-input-43-12db968aa80a> in check_province_code(province, country) 
   3  
   4   while num <3: 
----> 5     province = ''.join([str(0),province]) 
   6     num = num +1 
   7  
 
TypeError: 'str' object is not callable  

問題分析與排查:

   從錯(cuò)誤信息分析, str不是一個(gè)可調(diào)用的對(duì)象,可是之前確實(shí)可以調(diào)用的,且在python的api文檔中,其是python內(nèi)置的一個(gè)函數(shù)呀, 怎么不能用了呢?

 還是繼續(xù)驗(yàn)證一下吧。

   在命令行下執(zhí)行str(123),將數(shù)字轉(zhuǎn)換為string:

>>> str(1233) 
--------------------------------------------------------------------------- 
TypeError                 Traceback (most recent call last) 
<ipython-input-45-afcef5460e92> in <module>() 
----> 1 str(1233) 
 
TypeError: 'str' object is not callable  

 這下問題定義清楚了,原來沒有了str,仔細(xì)想了想原來剛才在定義變量的時(shí)候,隨機(jī)使用str,所以就被覆蓋了str函數(shù)。進(jìn)行了類似以下的操作:

str = '123' 

恢復(fù)默認(rèn)的str函數(shù)

   重新啟動(dòng)一下python應(yīng)用,移除str被覆蓋的代碼部分即可。

總結(jié)

  在python中內(nèi)置了很多的函數(shù)和類,在自己定義變量的時(shí)候,切記不要覆蓋或者和他們的名字重復(fù)。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論