Hope someone can help explain why I keep getting the following error in Python.
Python version: Python 3.10.4
OS: Windows 11 in a vm
Steps to reproduce the error in REPL or JuyterLab.
def minmax(items):
return min(items), max(items)
lst = [98, 34, 78, 1, 0, -10, -19, -1]
(min, max) = minmax(lst) # **no error**
min, max = minmax(lst) # **error**. after this point I cannot call minmax without an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in minmax
TypeError: 'int' object is not callable
'int' object is not callable
(min, max) = minmax(lst) # **error**
a, b = minmax(lst) # **error**
min, max = minmax(lst) # **error**
restart REPL or restart the kernel in JypterLab will solve the problem. This kind of gives me a hint that the issue might have something to do with the Python binds the variable with an Object.
Request: Whoever takes the time to respond, please also include your thought process. For example, "I have seen this before and before of my experience I know …" or "I read the Python doc and saw … which led me to experiment and discover…"
I want to be able to debug these myself and want to understand the thougt process that was used to get to the answer.
What have I tried so far?
-REPL to experiment
-Jyputer notebook to experiment
-read https://careerkarma.com/blog/python-typeerror-int-object-is-not-callable/
>Solution :
In this line:
(min, max) = minmax(lst)
you’re assigning integers to min
and max
. Then, you try to call these integers here:
def minmax(items):
return min(items), max(items) # this line
Try this in your interactive console:
>>> min([1, 0, 2])
0
>>> min = 42
>>> min([1, 0, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>>
Request: Whoever takes the time to respond, please also include your thought process.
Whenever you see a traceback:
- Find the line it refers to
- Try to find all things that could trigger it.
So:
- The traceback is pointing to this line:
return min(items), max(items)
- The traceback says:
'int' object is not callable
. This means that you’re trying to call anint
object. You’re only callingmin
andmax
on this line, so it must be thatmin
ormax
are integers. How could it be? - You might notice that later, you assign to
min
andmax
in your code.