‘str’ object is not callable – Python Error

Total
0
Shares

Python throws the error, ‘str’ object is not callable, when you use reserved functions like str() as local or global variable.

Let’s consider this code example –

avengerMoviesCount = 4
str = str(avengerMoviesCount)

numberOfInfinityStones = 6
convertToString = str(numberOfInfinityStones)

Did you get what’s wrong with this code? This code will throw the error – ‘str’ object is not callable. It is because of this line –

str = str(avengerMoviesCount)

str is a built in function of Python and we should not use it as a variable name. What happened is that after the assignment, str became a string variable and lost its property of being a function.

Now if you try to call str as a function, then Python will throw the str not callable error.

Here is the list of all the built-in functions, you should avoid using as local variables –

abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
delattr() hash() memoryview() set()  

    Tweet this to help others

Live Demo

Open Live Demo