开发者

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

开发者 https://www.devze.com 2023-03-28 18:28 出处:网络
This question already has answers here: 开发者_如何学编程 Why does code like `str = str(...)` cause a TypeError, but only the second time?
This question already has answers here: 开发者_如何学编程 Why does code like `str = str(...)` cause a TypeError, but only the second time? (20 answers) Closed 3 months ago.

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


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.


Because you've probably overwritten the str function by calling your own variable str.

0

精彩评论

暂无评论...
验证码 换一张
取 消