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 = ''.join([str(0),province]) num = num +1 return country + province
Running error message:
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
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: 'str' object is not callable
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 = '123'
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!