Detailed explanation and solution to str is not callable problem in Python

高洛峰
Release: 2017-02-13 16:14:10
Original
1752 people have browsed it

Detailed explanation and solution to the problem of str is not callable in Python

Question:

In the Python code, during the running process, an error message was encountered:

Python code:

def check_province_code(province, country):
  num = len(province)
    
  while num <3:
    province = &#39;&#39;.join([str(0),province])
    num = num +1
    
  return country + province
Copy after login

Running error message:

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

Problem analysis and troubleshooting:

Analysis from the error message , str is not a callable object, but it could indeed be called before, and in the Python API document, it is a built-in function of Python. Why can't it be used?

Let’s continue to verify.

Execute str(123) on the command line to convert the number into string:

>>> str(1233)
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last)
<ipython-input-45-afcef5460e92> in <module>()
----> 1 str(1233)
  
TypeError: &#39;str&#39; object is not callable
Copy after login

Now the problem is clearly defined. It turns out there is no str. Think about it carefully. It turns out that when defining the variable just now, str was used randomly, so the str function was overwritten. Performed operations similar to the following:

str = &#39;123&#39;
Copy after login

Restore the default str function

Restart the python application and remove the code part where str is overwritten.

Summary

There are many functions and classes built into python. When defining variables yourself, remember not to overwrite or repeat their names.

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more detailed explanations and solutions to the problem of str is not callable in Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!