The Farmer Was Replaced

The Farmer Was Replaced

Rogal Dorn Jun 12, 2024 @ 7:55pm
Issues with global scope
How can you access global scope inside a function?
Here's a basic example of my issue. index is only scoped to increment() so I can't modify it from inside a function
index = 0 def increment(): index += 1 while True: print(index) increment() print(index)

Both print statements will output 0 instead of the before and after incrementing
Last edited by Rogal Dorn; Jun 12, 2024 @ 7:58pm
< >
Showing 1-12 of 12 comments
owenz Jun 12, 2024 @ 9:07pm 
From what I understand of the chomped down version of python there you would have to do something like the following.

index = 0 def increment(arg): return arg + 1 for i in range(100): if index % 10 == 0: quick_print(index) index = increment(index)


It seems the scoping rules are much stricter than in regular python. So you would have to do something like this if you want to have that particular variable in scope to work on it.
Rogal Dorn Jun 12, 2024 @ 10:20pm 
That's not really usable for my use case.

The Solution
The solution it seems is to use a dictionary to store your globals, I missed the Name Scopes doc which says to do:

globals = { "index":0 } def increment(): globals["index"] += 1 while True: print(globals["index"]) increment() print(globals["index"])

So reference types are global but value types aren't
Last edited by Rogal Dorn; Aug 9, 2024 @ 4:13am
befana666 Jun 13, 2024 @ 9:39am 
thx i was breaking my head over this
Lynsis Jun 15, 2024 @ 4:26am 
global variables only need to be placed in the "main" function window so you would need a new window for testing that that you have the global and the function in.

i.e
#Global variables Index = 0 #Function calls increment()
Rogal Dorn Jul 11, 2024 @ 11:52am 
Originally posted by Lynsis:
global variables only need to be placed in the "main" function window so you would need a new window for testing that that you have the global and the function in.

i.e
#Global variables Index = 0 #Function calls increment()
The name of the window shouldn't matter. And if you try that code you'll see it doesn't work
Last edited by Rogal Dorn; Jul 11, 2024 @ 12:02pm
TerroFlys Jul 16, 2024 @ 7:11am 
Originally posted by Rogal Dorn:
That's not really usable for my use case. The solution it seems is to use a dictionary to store your globals, I missed the Name Scopes doc which says to do:

globals = { "index":0 } def increment(): globals["index"] += 1 while True: print(globals["index"]) increment() print(globals["index"])

So reference types are global but value types aren't


Thanks! I did not know Dictionaries would work! You're a life saver!
It does irritate me that we cannot modify global variables though, that should be in the game for sure.
󠀡 Jul 19, 2024 @ 12:20pm 
So you just can't use global variables until you have dictionaries unlocked?! What a nuisance.

Nevermind... You can use global variable IF they are declared in the "main" window but not anywhere else..
Last edited by 󠀡; Jul 19, 2024 @ 12:23pm
Rogal Dorn Jul 23, 2024 @ 2:23am 
Originally posted by 󠀡:
So you just can't use global variables until you have dictionaries unlocked?! What a nuisance.

Nevermind... You can use global variable IF they are declared in the "main" window but not anywhere else..
I don't know where that's working for you, but for me, and according to the docs that doesn't work.
If you define a variable in a window and have other code in the scope of that window then it will work, but then that's not a global. As soon as you add a function and try to modify that variable it'll error as a new variable is created in the scope of that function. You can disable the error but it won't function the way you think it would.

Meaning the output of this code...:
num = 0 def increment(): num += 1 def test(): num += 1 print(num) increment() print(num) test() print(num)
Is:
1 1 0
Instead of:
1 2 2
Last edited by Rogal Dorn; Jul 23, 2024 @ 1:32pm
Mr. Koala Gun Jul 24, 2024 @ 6:31pm 
This has also been a pain point for me in trying to make a more complex set of functions for the maze solve. I can't even use dictionaries yet because I have to solve the maze first :(. Adding the global keyword would be a huge help.
DarkQuiet Aug 2, 2024 @ 2:33pm 
Seems like each function call get not reference or pointer but copy of global variable thats so
♥♥♥♥♥♥♥ stupid
Last edited by DarkQuiet; Aug 2, 2024 @ 2:43pm
THC R4wizard Aug 6, 2024 @ 2:15am 
If you don't have dictionaries yet, you can do something similar with arrays:

state = [ 0, # [0]: counter (by 5) 0, # [1]: counter (by 2) 0, # [2]: counter (sum) ] def test(): state[0] += 5 state[1] += 2 state[2] = state[0] + state[1]
Mr Tree Frog Aug 7, 2024 @ 8:35am 
The only way I was able to get globals to work (ish) was to return the value which actually turned out to make more spaghetti code. Part of my maze issue was I was passing values around and it the values were not updating properly.

It definitely failed more than it worked. I was pondering to see if I could get a class to work but decided not to dive into Python classes and refactor my problem with the tools I knew was there.
< >
Showing 1-12 of 12 comments
Per page: 1530 50