How can i fixTypeError: 'str' object is not callable?

∥☆過路亽.° 提交于 2019-12-01 14:44:06

You've overwritten the built-in list function with a string:

list=l.pop()

To fix this, use a different variable name, other than list. In general, you'll want to avoid shadowing built-ins when naming your variables. That is, don't name things list, map, dict, etc.

It's also good practice to name your variables after what's in them. So if you have list = ["apples", "oranges", "pears"], you might consider renaming it fruits = ["apples", "oranges", "pears"]. It really helps with code readability.

You've defined list to be a string:

list=l.pop()

So

list(zip(symbols,deck))

causes Python to complain that list is not callable.

The solution, of course, is to use descriptive variable names that do not shadow Python builtins.

This happened to me recently where I tried to call a function I had defined from within another function, but didn't notice that I also had a local variable in the calling fucntion with the same name as the function I was trying to call.

Due to the scope rules the call to the function was being interpreted as calling the local variable...hence the error message since a scalar variable of type 'str' (for example) is not callable.

So the essence of the problem is variables sharing the names of functions you need to call within their scope, whether they be functions you've defined, those included in imported modules or Python built-ins.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!