a good subsitute for global variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

a good subsitute for global variables

I'm trying to run a portion of a program that tells me if my tank in a well is full or not. It seems like the "global" part of my level command is garbage-collecting my local command. Is there a way to have such a continuous survey using min/max or the class method instead of global variables?

8th Oct 2017, 2:50 AM
Nel Kerani
Nel Kerani - avatar
7 Answers
+ 4
You shouldn't lose variables unless they fall out of scope or are overwritten / reused. Python keeps reference counts and once userspace can no longer reach something (references only exist in system space), it is garbage-collected. A local scope (like: function variable) issue could be solved by adding a global with a different name (use with return like @Timo writes) A global scope issue could involve checking for 'del' (delete) or again adding new names. Classes give you the convenience of using the same name at different instants, but that may be a lot of overhead if a simple list (array) / dictionary (named values) will do. Is there any way to post a little code in CodePlayground, or... Do you want someone to discuss one of these data containers more?
8th Oct 2017, 4:44 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Ah, you're threading. Without saying this is related (I just have spotty network right now so say it while I can), you might like an approachable discussion of the global interpreter lock, which can affect order of operations: Grok the GIL: How to write fast and thread-safe Python https://opensource.com/article/17/4/grok-gil "When do threads switch? Whenever a thread begins sleeping [line 69 in daemon, line 96 in non-daemon] or awaiting network I/O, there is a chance for another thread to take the GIL and execute Python code..."
8th Oct 2017, 5:04 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
I am not sure if I get it completely but couldn't you just assign the value to the parts where you need it by returning it? So if you have your function check_tank return the calculated value and your function that provides the output get it by assigning for example: tank_status = check_well()? If it is in different classes or modules you would do course have to edit the function call but the basic idea should not change.
8th Oct 2017, 2:57 AM
Timo
Timo - avatar
+ 2
Hi Guys! Thank you so much for your help. This is already helpful. I posted the code on sololearn at code.sololearn.com/ciB2MAC5OxuG/#py
8th Oct 2017, 4:52 AM
Nel Kerani
Nel Kerani - avatar
+ 1
Also that's what I've been having problems with - those two lines are producing the errors. So if this article helps me remedy that, I'll be really grateful!
8th Oct 2017, 5:35 AM
Nel Kerani
Nel Kerani - avatar
0
post the code here..
8th Oct 2017, 3:54 AM
sayan chandra
sayan chandra - avatar
0
So helpful. You're the best!!
8th Oct 2017, 5:15 AM
Nel Kerani
Nel Kerani - avatar