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..."
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 .
Because there are no variable definitions in Python, when you write
a function Python does a static analysis of it to decide which
variables are local and which are not. If there's an assignment to a variable, it is a local variable. _Regardless_ of whether that
assignment has been executed, or gets executed at all (eg in an
if-statement branch which doesn't fire).
From the page https://www.geeksforgeeks.org/global-local-variables-python/
Unfortunately (presumably thanks to SEO) the enshittification ofAnd not just Google, I just tried bing, yahoo and duckduckgo
Google has reached the point where searching for info on things like
Python name scope, the first page of links are to worthless sites like geeksforgeeks.
Jacob,
Please reduce the problem to a small code-set which reproduces the
problem. If we can reproduce same, then that tells us something. At
the very least, we can experiment without having to expend amounts of
time in a (likely faulty) bid to reproduce the same environment.
Also, code is the ultimate description!
Perhaps start with a small experiment:
- after l_servers is created, print its id()
- after the global statement, print its id()
- after the clear/reassignment, print its id()
Is Python always working with the same list?
Please advise...
On 6/03/24 07:13, Jacob Kruger via Python-list wrote:
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..."
Ok, simpler version - all the code in a simpler test file, and working
with two separate variables to explain exactly what am talking about:
# start code
from datetime import datetime, timezone, timedelta
from copy import copy
# initialise original values
dt_expiry = datetime.strptime("1970-01-01 00:00", "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
l_test = [1, 2, 3]
def do_it():
global dt_expiry, l_test # asked python to refer to global
variables for both
# assign new value immediately
dt_expiry = datetime.now()+timedelta(minutes=5)
print(dt_expiry.strftime("%Y-%m-%d %H:%M")) # just to show new
value has been assigned
# grab copy of list for re-use of items
l_temp = copy(l_test)
# following line means l_test will later on retain value in global scope because it was manipulated inside function instead of just
assigned new value
l_test.clear()
# replace original set of values
for i in l_temp: l_test.append(i)
# add new item
l_test.append(99)
# end of do_it function
# end code
If you import the contents of that file into the python interpreter, dt_expiry will start off as "1970-01-01 00:00", and, if you execute
do_it function, it will print out the new value assigned to the
dt_expiry variable inside that function, but if you then again check
the value of the dt_expiry variable afterwards, it's reverted to the
1970... value?
If I take out the line that removes values from l_test #
l_test.clear() # before appending new value to it, then it will also
not retain it's new/additional child items after the function exits,
and will just revert back to [1, 2, 3] each and every time.
In other words, with some of the variable/object types, if you use a function that manipulates the contents of a variable, before then re-assigning it a new value, it seems like it might then actually update/manipulate the global variable, but, either just calling purely content retrieval functions against said objects, or assigning them
new values from scratch seems to then ignore the global scope
specified in the first line inside the function?
Hope this makes more sense
Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."
On 2024/03/05 20:23, dn via Python-list wrote:--- Synchronet 3.20a-Linux NewsLink 1.114
Jacob,
Please reduce the problem to a small code-set which reproduces the
problem. If we can reproduce same, then that tells us something. At
the very least, we can experiment without having to expend amounts of
time in a (likely faulty) bid to reproduce the same environment.
Also, code is the ultimate description!
Perhaps start with a small experiment:
- after l_servers is created, print its id()
- after the global statement, print its id()
- after the clear/reassignment, print its id()
Is Python always working with the same list?
Please advise...
On 6/03/24 07:13, Jacob Kruger via Python-list wrote:
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..."
Ok, simpler version - all the code in a simpler test file, and working
with two separate variables to explain exactly what am talking about:
If you import the contents of that file into the python interpreter, dt_expiry will start off as "1970-01-01 00:00", and, if you execute
do_it function, it will print out the new value assigned to the
dt_expiry variable inside that function, but if you then again check the value of the dt_expiry variable afterwards, it's reverted to the 1970... value?
If I take out the line that removes values from l_test # l_test.clear()
# before appending new value to it, then it will also not retain it's new/additional child items after the function exits, and will just
revert back to [1, 2, 3] each and every time.
In other words, with some of the variable/object types, if you use a function that manipulates the contents of a variable, before then re-assigning it a new value, it seems like it might then actually update/manipulate the global variable, but, either just calling purely content retrieval functions against said objects, or assigning them new values from scratch seems to then ignore the global scope specified in
the first line inside the function?
Hope this makes more sense
On 05/03/2024 22:46, Grant Edwards via Python-list wrote:
Unfortunately (presumably thanks to SEO) the enshittification ofAnd not just Google, I just tried bing, yahoo and duckduckgo
Google has reached the point where searching for info on things like
Python name scope, the first page of links are to worthless sites like
geeksforgeeks.
and they are all the same. Not a one listed anything from
python.org on the first page... In fact it didn't even appear
in the first 100 listings, although wikipedia did manage an
entry, eventually.
Ok, simpler version - all the code in a simpler test file, and working
with two separate variables to explain exactly what am talking about:
# start code
from datetime import datetime, timezone, timedelta
from copy import copy
# initialise original values
dt_expiry = datetime.strptime("1970-01-01 00:00", "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
l_test = [1, 2, 3]
def do_it():
global dt_expiry, l_test # asked python to refer to global
variables for both
# assign new value immediately
dt_expiry = datetime.now()+timedelta(minutes=5)
print(dt_expiry.strftime("%Y-%m-%d %H:%M")) # just to show new
value has been assigned
# grab copy of list for re-use of items
l_temp = copy(l_test)
# following line means l_test will later on retain value in global scope because it was manipulated inside function instead of just
assigned new value
l_test.clear()
# replace original set of values
for i in l_temp: l_test.append(i)
# add new item
l_test.append(99)
# end of do_it function
# end code
If you import the contents of that file into the python interpreter, dt_expiry will start off as "1970-01-01 00:00", and, if you execute
do_it function, it will print out the new value assigned to the
dt_expiry variable inside that function, but if you then again check the value of the dt_expiry variable afterwards, it's reverted to the 1970... value?
If I take out the line that removes values from l_test # l_test.clear()
# before appending new value to it, then it will also not retain it's new/additional child items after the function exits, and will just
revert back to [1, 2, 3] each and every time.
In other words, with some of the variable/object types, if you use a function that manipulates the contents of a variable, before then re-assigning it a new value, it seems like it might then actually update/manipulate the global variable, but, either just calling purely content retrieval functions against said objects, or assigning them new values from scratch seems to then ignore the global scope specified in
the first line inside the function?
Hope this makes more sense
Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."
On 2024/03/05 20:23, dn via Python-list wrote:
Jacob,
Please reduce the problem to a small code-set which reproduces the
problem. If we can reproduce same, then that tells us something. At
the very least, we can experiment without having to expend amounts of
time in a (likely faulty) bid to reproduce the same environment.
Also, code is the ultimate description!
Perhaps start with a small experiment:
- after l_servers is created, print its id()
- after the global statement, print its id()
- after the clear/reassignment, print its id()
Is Python always working with the same list?
Please advise...
On 6/03/24 07:13, Jacob Kruger via Python-list wrote:
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..."
1970-01-01 00:00:00+00:00from scoping2 import *
print(dt_expiry)
2024-03-06 18:12do_it()
1970-01-01 00:00:00+00:00print(dt_expiry)
... global dt_expiryfrom datetime import datetime, timezone, timedelta
dt_expiry = datetime.strptime("1970-01-01 00:00", "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
def do_it():
1970-01-01 00:00:00+00:00print(dt_expiry)
2024-03-06 18:20do_it()
2024-03-06 18:20:03.909825print(dt_expiry)
On 3/6/2024 7:55 AM, Jacob Kruger via Python-list wrote:
Ok, simpler version - all the code in a simpler test file, and
working with two separate variables to explain exactly what am
talking about:
# start code
from datetime import datetime, timezone, timedelta
from copy import copy
# initialise original values
dt_expiry = datetime.strptime("1970-01-01 00:00", "%Y-%m-%d
%H:%M").replace(tzinfo=timezone.utc)
l_test = [1, 2, 3]
def do_it():
global dt_expiry, l_test # asked python to refer to global
variables for both
# assign new value immediately
dt_expiry = datetime.now()+timedelta(minutes=5)
print(dt_expiry.strftime("%Y-%m-%d %H:%M")) # just to show new
value has been assigned
# grab copy of list for re-use of items
l_temp = copy(l_test)
# following line means l_test will later on retain value in
global scope because it was manipulated inside function instead of
just assigned new value
l_test.clear()
# replace original set of values
for i in l_temp: l_test.append(i)
# add new item
l_test.append(99)
# end of do_it function
# end code
If you import the contents of that file into the python interpreter,
dt_expiry will start off as "1970-01-01 00:00", and, if you execute
do_it function, it will print out the new value assigned to the
dt_expiry variable inside that function, but if you then again check
the value of the dt_expiry variable afterwards, it's reverted to the
1970... value?
Not when I run your code. With a little annotation added to the print statements I get (I added the import statements to make it run, and I
used the same date-time formatting for all three print statements):
List before: [1, 2, 3]
start: 1970-01-01 00:00
inside after reassignment: 2024-03-06 08:57
outside after: 2024-03-06 08:57
List after: [1, 2, 3, 99]
As an aside, you have gone to some trouble to copy, clear, and
reconstruct l_test. It would be simpler like this (and you wouldn't
have to import the "copy" library):
l_temp = l_test[:]
l_test = []
Instead of those lines and then this:
for i in l_temp: l_test.append(i)
you could achieve the same thing with this single statement:
l_test = l_test[:]
If I take out the line that removes values from l_test #
l_test.clear() # before appending new value to it, then it will also
not retain it's new/additional child items after the function exits,
and will just revert back to [1, 2, 3] each and every time.
In other words, with some of the variable/object types, if you use a
function that manipulates the contents of a variable, before then
re-assigning it a new value, it seems like it might then actually
update/manipulate the global variable, but, either just calling
purely content retrieval functions against said objects, or assigning
them new values from scratch seems to then ignore the global scope
specified in the first line inside the function?
Hope this makes more sense
Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."
On 2024/03/05 20:23, dn via Python-list wrote:
Jacob,
Please reduce the problem to a small code-set which reproduces the
problem. If we can reproduce same, then that tells us something. At
the very least, we can experiment without having to expend amounts
of time in a (likely faulty) bid to reproduce the same environment.
Also, code is the ultimate description!
Perhaps start with a small experiment:
- after l_servers is created, print its id()
- after the global statement, print its id()
- after the clear/reassignment, print its id()
Is Python always working with the same list?
Please advise...
On 6/03/24 07:13, Jacob Kruger via Python-list wrote:
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..."
Op 6/03/2024 om 13:55 schreef Jacob Kruger via Python-list:
If you import the contents of that file into the python interpreter,
[...]
What exactly to you mean by "import the contents of that file into the python interpreter"? Other people have put your code in a script,
executed it, and saw it working as expected. I pasted in IPython, and likewise saw it working as expected, and the same with IDLE. It seems
to me you must be doing something different from us; maybe the way you execute that code might be the key to this whole confusion.
C:\temp\py_try>python
Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
from scoping2 import *
On 3/6/24 08:28, Jacob Kruger via Python-list wrote:--- Synchronet 3.20a-Linux NewsLink 1.114
C:\temp\py_try>pythonv.1937 64 bit (AMD64)] on win32
Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC
Type "help", "copyright", "credits" or "license" for more information.
>>> from scoping2 import *
And it becomes clear: only do `from ... import *` when the module has
been specifically designed to support that.
If you were to also do `import scoping2` and, after calling `do_it()`, `print(scoping2.dt_expiry)`, you would see that it had changed.
I know there are good explanations for how variables and names work in Python, but I couldn't find any at the moment. Sorry.
--
~Ethan~
Ah yes, that explains what's happening. After that statement, the name dt_expiry in the current namespace is bound to the same object that thefrom scoping2 import *
Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list:
from scoping2 import *
[...]
I would advice not to use 'import *', if at all possible, for multiple reasons, one of which is to prevent problems like this.
I would also advice not to use global variables from other modules
directly, and in fact would advice to minimize the use of globals in
general as much as possible. If you need to keep state between
methods, it might be better to use a class.
On 2024-03-06, Roel Schroeven via Python-list <python-list@python.org> wrote:
Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list:
from scoping2 import *
[...]
I would advice not to use 'import *', if at all possible, formultiple > reasons, one of which is to prevent problems like this.
Unfortunately, many (most?) tutorials for particular modules (and even example code in the Python documentation itself) are all written
assuming that you do "from <module> import *". It saves the tutorial
write a few keystrokes, but causes untold trouble for people who learn incorrectly that "from <module> import *" is the proper way to do
things.
So, yes, know this comes across like some form of a scam/joke, or list-garbage, since it doesn't make any sense to me at all, but stillSome of the facts, such as HOW the code was being executed were missing
just wondering if missing something, or should I shift over to 3.12 to
see if if works differently, or just try reinstalling 3.11 from scratch,
or should I retry the above in something like the VS code console, or a different python console, etc.?
On 2024-03-07, dn via Python-list <python-list@python.org> wrote:
The idea of importing a module into the REPL and then (repeatedly)Unless the code is intended to be used as a module, 'import'ing it into
manually entering the code to set-up and execute is unusual (surely type
such into a script (once), and run that (repeatedly). As you say, most
of us would be working from an IDE and hitting 'Run'. Am wondering why
you weren't - but it's not important.
the REPL doesn't make sense.
A simple example:
---------------------------testit.py------------------------------
x = 'x'
y = 'y'
def foo():
global y
print("hi")
x = 'X'
y = 'Y'
print(x)
print(y) ------------------------------------------------------------------
The usual method to play with that interactively is
$ python -i testit.py
>>> x
'x'
>>> y
'y'
>>> foo()
hi
X
Y
>>> x
'x'
>>> y
'Y'
>>>
As we've seen, doing a 'from testit.py import *' doesn't let you test
what the OP was trying to test. Doing 'import testit.py' gets you
closer, but it's a hassle to test code that way. The right thing to do
is 'python -i <filename>' (or the equivalent button/option in an IDE).
https://docs.python.org/3/tutorial/interpreter.html
If you intended to use testit.py as a module, and wanted to experiment
with its behavior as a module, then go ahead and import it. But, don't
do 'from testit.py import *' until
1. you know how that differs from 'import testit.py'
and
2. you want to use that difference
So, this does not make sense to me in terms of the following snippet
from the official python docs page: >https://docs.python.org/3/faq/programming.html
"In Python, variables that are only referenced inside a function are >implicitly global. If a variable is assigned a value anywhere within
the function’s body, it’s assumed to be a local unless explicitly >declared as global."
So, I would then assume that if I explicitly include a variable name
inside the global statement, then even just assigning it a new value
should update the variable in the global context, outside the
function?
Yes. Note that the "global" namespace is the module in which the
function is defined.
One might argue that "global" isn't a good choice for what to call the
scope in question, since it's not global. It's limited to that source
file. It doesn't make sense to me to call a binding "global", when
there can be multile different "global" bindings of the same name.
On Sat, 9 Mar 2024 at 00:51, Grant Edwards via Python-list
<python-list@python.org> wrote:
One might argue that "global" isn't a good choice for what to call the
scope in question, since it's not global. It's limited to that source
file. It doesn't make sense to me to call a binding "global", when
there can be multile different "global" bindings of the same name.
Most "globals" aren't global either, since you can have different
globals in different running applications.
On 2024-03-08, Chris Angelico via Python-list <python-list@python.org> wrote:
On Sat, 9 Mar 2024 at 00:51, Grant Edwards via Python-list
<python-list@python.org> wrote:
One might argue that "global" isn't a good choice for what to call the
scope in question, since it's not global. It's limited to that source
file. It doesn't make sense to me to call a binding "global", when
there can be multile different "global" bindings of the same name.
Most "globals" aren't global either, since you can have different
globals in different running applications.
To me, "global" has always been limited to within a single
process/address space, but that's probably just bias left over from C/Pascal/FORTRAN/assembly/etc. It never occurred to me that a global
called "X" in one program on one computer would be the same as a
global called "X" in a different program on a different computer
somewhere else on the "globe".
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 915 |
Nodes: | 10 (1 / 9) |
Uptime: | 19:08:11 |
Calls: | 12,168 |
Calls today: | 4 |
Files: | 186,520 |
Messages: | 2,233,870 |