How To Fix Python Error Typeerror: ‘str’ Object Is Not Callable

When i develop python source code, i want to use python built-in function str() to conver a number variable to string type. But i meet below error Typeerror: ‘str’ Object Is Not Callable. 

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

This error confused me a lot for some time. But i finally fix it after google it. The reason for this error is because i had use a variable name str in the same python interpreter, and i had assign string value to that variable str. So the python interpreter treat str as a variable name other than built-in function name str(). So it throw out TypeError: ‘str’ object is not callable. Below is the full python source  code.

>>> global str
>>>
# some times ago, i assign string 'hello' to str variable, so python interpreter think str is a string variable.
>>> str = 'hello'
>>>
......
......
......
>>> x= 1.99
# so when i invoke str() function, the interpreter think str is a string variable then error happened.
>>> str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Conclusion : Do not declare python variable name same with python built-in function name, keyword etc.

READ :   Python Parse Emails And Attachments From POP3 Server Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.