Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference
Jacob Kruger
jacob.kruger.work at gmail.com
Tue Mar 5 13:13:21 EST 2024
Hi there
Working with python 3.11, and, issue that confused me for a little
while, trying to figure out what was occurring - unless am completely
confused, or missing something - was that, for example, when having
pre-defined a variable, and then included it in the global statement
inside a function, that function was still referring to a completely
local instance, without manipulating outside variable object at all
unless I first executed a form of referral to it, before then possibly
assigning a new value to it.
Now, this does not seem to occur consistently if, for example, I just
run bare-bones test code inside the python interpreter, but consistently
occurs inside my actual testing script.
Basically, in a file with python code in that am using for a form of
testing at the moment, at the top of the file, under all the import
statements, I initiate the existence of a list variable to make use of
later:
# code snippet
l_servers = []
# end of first code snippet
Then, lower down, inside a couple of different functions, the first line
inside the functions includes the following:
# code snippet
global l_servers
# end code snippet
That should, in theory, mean that if I assign a value to that variable
inside one of the functions, it should reflect globally?
However, it seems like that, while inside those functions, it can be
assigned a new list of values, but if I then return to the scope outside
the functions, it has reverted back to being an empty list = []?
The issue seems to specifically (or not) occur when I make a call to one
function, and, in the steps it's executing in one context, while it's
not doing anything to the list directly, it's then making a call to the
second function, which is then meant to repopulate the list with a brand
new set of values.
Now, what almost seems to be occurring, is that while just manipulating
the contents of a referenced variable is fine in this context, the
moment I try to reassign it, that's where the issue is occurring .
Here are relevant excerpts from the file:-
# start code
# original assignation in main part of file
l_servers = []
# function wich is initially being executed
def interact():
global l_servers
# extra code inbetween choosing what to carry out
# ...
# end of other code
bl_response, o_out = list_servers()
if bl_response: # just make sure other function call was successful
l_servers.clear() # first make reference to global variable
for srv in o_out: l_servers.append(srv) # now re-populate items
# end code snippet from inside interact function
# end of interact function
# end of code snippet
That other function being called from within, list_servers() was
initially just trying to populate the values inside the global list
variable itself, but was ending up in a similar fashion - reverting to
initial empty value, but, the above now seems to work, as long as I
first make reference to/manipulate/work with global variable instead of
just trying to reassign it a brand new value/set of items?
So, am I missing something obvious, have I forgotten about something
else - yes, know that if was working from within an embedded function, I
might need/want to then use the nonlocal statement against that variable
name, but, honestly, just not sure how this can be occurring, and, it's
not just with this one list variable, etc.?
If I try simple test code from within the python interpreter, using
different types of variables, this does also not seem to be the same all
the time, but, don't think it can relate to an iterable like a list, or
else, just in case, here is the code snippet with all the import
statements from the top of that file, in case something could be
overriding standard behaviour - not likely in this context, but, really
not sure what's occurring:
# import code snippet
import requests, time
from requests.auth import HTTPBasicAuth
import psutil as psu
import pytz
import bcrypt
from copy import copy
from datetime import datetime, timedelta, timezone
from dateutil.parser import parse
# end of import snippet
Thanks if you have any ideas/thoughts on the matter
Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."
More information about the Python-list
mailing list