Python Forum
Spyder Quirk? global variable does not increment when function called in console
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spyder Quirk? global variable does not increment when function called in console
I did not see that Spyder had a forum so I tried here. If I should go somewhere else then please let me know.

The following code works as expected when run in a file using Spyder:

v= 0
def inc():    
    global v
    v +=1
    return v

Spyder Console:
In [135]: runfile('', wdir='/')

As expected, variable v prints 3 but if I call inc() from the Spyder console it continues to increment the return value but the variable v is not updated.

Spyder Console:

In [136]: inc()
Out[136]: 4

In [137]: v
Out[137]: 3

In [138]: inc()
Out[138]: 5

In [139]: v
Out[139]: 3

Does anyone know why this happens? Any insight would be appreciated.

Very Respectfully from an Unladen Swallow
The value of v in the program updating. That is shown by the value returned by inc(). For some reason Spyder is looking elsewhere when you ask for the value of v.

This all works as expected in IDLE. What happens if you set the value of v from the consle and call inc()?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Character Increment AnokhiRaaz 1 836 Apr-22-2021, 04:29 AM
Last Post: buran
  Increment text files output and limit contains Kaminsky 1 903 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Variable scope - "global x" didn't work... ptrivino 5 893 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Finding global extrema of oscillating function JoeRogan 0 547 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  What is this formatting called? Mark17 2 544 Dec-14-2020, 08:42 PM
Last Post: snippsat
  How to make global list inside function CHANKC 6 1,032 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 768 Nov-07-2020, 08:59 AM
Last Post: buran
  passing variable to function Rejoice 4 1,215 Sep-11-2020, 03:27 AM
Last Post: Pleiades
  [split] Creating a variable as a function DPaul 23 2,354 Sep-07-2020, 05:20 PM
Last Post: DPaul
  Creating a variable as a function JarredAwesome 4 1,319 Sep-06-2020, 05:08 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020