Weirdness calling str() to convert integer to string in Python 3?

blueplastic picture blueplastic · Aug 15, 2011 · Viewed 58.2k times · Source

Why is this giving me an error?

>>> variable = str(21)

Traceback (most recent call last):
  File "<pyshell#101>", line 1, in <module>
    variable = str(21)
TypeError: 'str' object is not callable

Answer

Jeremy picture Jeremy · Aug 15, 2011

That code alone won't give you an error. For example, I just tried this:

~ $ python3.2
>>> variable = str(21)
>>> variable
'21'

Somewhere in your code you're defining that str = something else, masking the builtin definition of str. Remove that and your code will work fine.