4

I'm trying to use the max function in Python 3,6:

print('Maximum is:', max(1, 3, 2, 5, 4))

And the result is

  File "E:/ProgramyRobione/untitled1.py", line 2, in <module>
    print('Maximum is:', max(1, 3, 2, 5, 4))

TypeError: 'int' object is not callable

I'm using Spyder and that line is all that code should do.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Argos
  • 63
  • 7
  • 9
    Evidently you've assigned `max = ` somewhere; don't do that. – jonrsharpe Dec 10 '17 at 11:51
  • 1
    @dkato actually they don't - https://docs.python.org/3/library/functions.html#max. Also it's helpful to describe what you're actually *doing* with that - *"pass a list or tuple of numbers"*, to relate the syntax to something the OP can search for. – jonrsharpe Dec 10 '17 at 11:53
  • i didn't assign max anywhere, it's the only line in the whole code. max([1, 3, 2, 5, 4]) is generating SyntaxError: unexpected EOF while parsing, and max[(1, 3, 2, 5, 4)] is generating TypeError: 'int' object is not subscriptable – Argos Dec 10 '17 at 11:54
  • The error `'int' object is not subscriptable` tells you, again, that `max` is an integer not the built-in function. `unexpected EOF while parsing` is unexpected, though, and not what I get locally - clearly you have problems your example doesn't show us. Give a [mcve]. – jonrsharpe Dec 10 '17 at 11:55
  • @jonrsharpe Sorry, i deleted. – dkato Dec 10 '17 at 11:55
  • @Argos, `In [1]: print('Maximum is:', max(1, 3, 2, 5, 4))` `Maximum is: 5` – user3053452 Dec 10 '17 at 11:55
  • It's the second line of the file - have you got a `from something import *`? – Jon Clements Dec 10 '17 at 11:56
  • But it's written in documentation that it is built in function... Nah, it's second line, but first is empty. – Argos Dec 10 '17 at 11:57
  • 2
    It is a builtin but you've got something that isn't - can you show the first line of your code? – Jon Clements Dec 10 '17 at 11:58
  • @Argos `max([1, 3, 2, 5, 4])` and `max[(1, 3, 2, 5, 4)]` are not the same, you get a `TypeError` with the latter. – srikavineehari Dec 10 '17 at 11:59
  • First line is empty. I deleted it and nothing changed. And i know that brackets matter, but im using example from web and the code isn't working. – Argos Dec 10 '17 at 12:02
  • Unless you're using some very exotic Python interpreter, or some setup script that overwrites your `max` built-in, if you have on the first line of your script: `print(max(1, 2, 3, 4, 5))` it will print out `5`. – zwer Dec 10 '17 at 12:03
  • @zwer print(max(1, 2, 3, 4, 5)) Is still not working, so i guess there is some deeper problem... – Argos Dec 10 '17 at 12:05
  • Can you show the result of `print(type(max))` in the line? It should be . – dkato Dec 10 '17 at 12:05
  • 2
    It doesn't have to be an exotic Python interpreter. This is actually very easy to recreate if you defined `max = some_int` in the iPython console of Spyder and then try to run your script since there is a global namespace. If you're using an IDE which supports this, try restarting the console (usually ctrl + .) – roganjosh Dec 10 '17 at 12:06
  • 1
    @dkato btw - you can delete your own comments... just hover over them and you'll get an `(x)` at the end of it - click that and it's gone - no need to edit it to say it's deleted :) – Jon Clements Dec 10 '17 at 12:06
  • OKAY, reseting namespace worked, I forgot that spyder saves old variables :p THANKS FOR HELP! – Argos Dec 10 '17 at 12:08
  • @JonClements Thanks for you guide. I will do so next time. – dkato Dec 10 '17 at 12:11
  • @Argos, there you are. You have assigned an `int` value to a variable named `max` or defined a function that returns an `int` in your code etc. other than just `print('Maximum is:', max(1, 3, 2, 5, 4))`. – srikavineehari Dec 10 '17 at 12:11
  • Try in python shell – BoRRis Dec 10 '17 at 12:25

1 Answers1

10

This is a bit of an iPython headshot that comes with Spyder. I've fallen foul of this when copy/pasting random bits of code from SO to test only to find really odd behaviour several days later - variables defined in the iPython console will also be in the script's global namespace indefinitely.

There's two things you can do:

  1. A hard restart of the Kernel (ctrl + .)
  2. Follow up on this feature request thread where it's now possible to clear the namespace automatically every time you run a script.
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • 1
    (*Spyder maintainer here*) The feature to remove all variables before execution is already implemented in Spyder since its version **3.1**. – Carlos Cordoba Dec 10 '17 at 15:45
  • @CarlosCordoba I downloaded on this laptop after 1st December (I don't know the exact date but _definitely_ after that as I was abroad until then) and by default there is namespace interference. I acknowledge the feature in my answer but I definitely could recreate the OP's issue easily. Script --> console access is _awesome_ but I've had nothing but trouble from console --> script. – roganjosh Dec 10 '17 at 15:50
  • @CarlosCordoba I should acknowledge that I also think Spyder is awesome and it's my IDE of choice but having this turned on by default has caused some real debugging issues for me in the past. Maybe it's me, but I didn't expect it to pollute the namespace of my script at all by default. – roganjosh Dec 10 '17 at 15:55
  • 2
    @roganjosh, three things: 1. It's true that cleaning the namespace if off by default, but the option to turn that on is there now; 2. We also provide an evaluation model which runs each file in its dedicated console. Under that model, the namespace is cleaned every time a file is rerun, and you can activate it in `Run > Configuration per file` or `Preferences > Run` to make it global; 3. It's probably not intuitive to leave results from previous evaluation in the console and pollute your namespace with them, but people coming from Matlab are very accustomed to it. – Carlos Cordoba Dec 10 '17 at 16:50
  • @CarlosCordoba. Based on https://stackoverflow.com/q/58018003/2988730, I think that the expectation would be that there would be a third non-nuclear option: run the script in the current console, but don't contaminate the script's namespace when you're running it. – Mad Physicist Sep 19 '19 at 20:29
  • @MadPhysicist, that option was already implemented and it'll be part of Spyder 4 by default, i.e. running a script in an empty namespace so it doesn't pick up variables from the current namespace. – Carlos Cordoba Sep 21 '19 at 08:48
  • 1
    @CarlosCordoba. That's awesome. I'll try out the updated version ASAP. – Mad Physicist Sep 21 '19 at 11:08