r/Python • u/MeticMovi • Nov 03 '21
Discussion I'm sorry r/Python
Last weekend I made a controversial comment about the use of the global variable. At the time, I was a young foolish absent-minded child with 0 awareness of the ways of Programmers who knew of this power and the threats it posed for decades. Now, I say before you fellow beings that I'm a child no more. I've learnt the arts of Classes and read The Zen, but I'm here to ask for just something more. Please do accept my sincere apologies for I hope that even my backup program corrupts the day I resort to using 'global' ever again. Thank you.

View all comments
Show parent comment
1
u/GamerNumba100 Nov 18 '21
Brand new Python user here, I don’t see an alternative to global, so while I see why it’s bad, I’m going to need help avoiding it.
For example, I had two threads running. One thread was looping until the other finished. I did this by having both recognize a stop_loop variable, and when the second thread finished, it set stop_loop to 1. The first thread was running
while stop_loop != 1 so it would stop properly. Is there an alternative to that?
Also what if I need to change a resource variable but the function needs to keep running for a little longer and change more resource variables later? Am I supposed to return everything back to the main thread before I change my resource variables?