‘list’ object is not callable – Python Error

Total
0
Shares

Python throws the error ‘list’ object is not callable, when you have assigned a local or global variable as name ‘list’ somewhere in your code. Since list() is a built-in function, it should not be used as variable name otherwise it will lose its functional properties.

Consider this code example –

SuperHero = 'Captain America'
list = list(SuperHero)
# list has become a variable now
print(list)
# output: ['C','a','p','t','a','i','n',' ','A','m','e','r','i','c','a']

SuperVillain = 'Thanos'
list = list(SuperVillain)
# Error: 'list' object is not callable

Hope you got what’s wrong in the code. When you assigned list = list(SuperHero) it has become an array or characters. It lost its property of being a function. Now next time when you try to use it as function (list = list(SuperVillain)) you will get error.

The correct way of doing these things is to avoid using built-in function names as variables in your code. This will work perfectly fine –

SuperHero = 'Captain America'
list_SuperHero = list(SuperHero)

print(list_SuperHero)
# output: ['C','a','p','t','a','i','n',' ','A','m','e','r','i','c','a']

SuperVillain = 'Thanos'
list_SuperVillain = list(SuperVillain)
print(list_SuperVillain)
#output: ['T','h','a','n','o','s']

    Tweet this to help others

Here is the list of all built-in functions which should be avoided to be defined as 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()  

Why python allows to use built-in function as variables, if that will create problems?

Because most of the functions are not frequently used in majority of codes. For example, you will rarely use memoryview() function. If you want to use memoryview as local variable name then you can easily use that without even knowing that a function exists with this name. Python do not want to restrict you otherwise there are so many things to restrict and it will get very hard to write programs.

Live Demo

Demo might not work in private window (incognito)

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
python run bash script in background

python run bash script in background

Table of Contents Hide IntroductionTerminating background script after a timeoutHow to check if process is still running or terminated?How to get output from process?ConclusionLive DemoRelated Posts: Python subprocess library is…
View Post