Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"It's when they say 2 + 2 = 5 that I begin to argue." -- Eric Pepke


devel / comp.lang.python / Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

SubjectAuthor
o Re: Variable scope inside and outside functions - global statement being overriddn

1
Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

<mailman.29.1709663033.3452.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29574&group=comp.lang.python#29574

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonList@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: Variable scope inside and outside functions - global statement
being overridden by assignation unless preceded by reference
Date: Wed, 6 Mar 2024 07:23:41 +1300
Organization: DWM
Lines: 164
Message-ID: <mailman.29.1709663033.3452.python-list@python.org>
References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<0ccad7a9-eaba-48e6-b972-d89e5a930c11@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de QzX+5kWGS+X3Kvgy7k43dQq5s+EnVDnRYUTpiJu7EtWw==
Cancel-Lock: sha1:IE4XDWrsrG7UhrUysXyl1HKYn+w= sha256:PhZhJDCD5sehHD6w+dzENGjfDpAiGbJBLUx1eSrt3vU=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=iwqka6ZQ; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'variable':
0.05; 'moment,': 0.07; '=dn': 0.09; 'datetime': 0.09;
'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
0.09; 'itself,': 0.09; 'meant': 0.09; 'parse': 0.09; 'populate':
0.09; 'ultimate': 0.09; 'values.': 0.09; 'steps': 0.11; 'import':
0.15; 'problem.': 0.15; 'behaviour': 0.16; 'confused': 0.16;
'confused,': 0.16; 'directly,': 0.16; 'executed': 0.16;
'functions,': 0.16; 'initiate': 0.16; 'iterable': 0.16;
'manipulating': 0.16; 'message-id:@DancesWithMice.info': 0.16;
'not)': 0.16; 'psutil': 0.16; 'received:51.254': 0.16;
'received:51.254.211': 0.16; 'received:51.254.211.219': 0.16;
'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'reproduce':
0.16; 'script.': 0.16; 'should,': 0.16; 'something.': 0.16;
'subject:being': 0.16; 'subject:reference': 0.16; 'timezone':
0.16; 'values,': 0.16; 'variable,': 0.16; 'variables,': 0.16;
'while,': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python': 0.16;
'values': 0.17; 'instead': 0.17; 'figure': 0.19; 'reduce': 0.19;
'to:addr:python-list': 0.20; 'issue': 0.21; "what's": 0.22;
'code': 0.23; 'run': 0.23; 'list,': 0.24; 'anything': 0.25;
'actual': 0.25; 'seems': 0.26; 'interact': 0.26; 'object': 0.26;
'else': 0.27; 'local': 0.27; 'function': 0.27; 'example,': 0.28;
'it,': 0.29; 'header:User-Agent:1': 0.30; 'seem': 0.31;
'header:Organization:1': 0.31; 'think': 0.32; 'amounts': 0.32;
'else,': 0.32; 'empty': 0.32; 'python-list': 0.32; 'requests,':
0.32; 'same,': 0.32; 'unless': 0.32; 'received:192.168.1': 0.32;
'but': 0.32; 'there': 0.33; 'same': 0.34; 'mean': 0.34; 'header
:In-Reply-To:1': 0.34; 'trying': 0.35; 'fine': 0.35; 'reflect':
0.35; 'yes,': 0.35; 'also,': 0.36; 'functions': 0.36; 'possibly':
0.36; 'work,': 0.36; 'those': 0.36; 'couple': 0.37; 'missing':
0.37; 'main': 0.37; 'really': 0.37; 'using': 0.37; "it's": 0.37;
'received:192.168': 0.37; 'file': 0.38; 'could': 0.38; 'thanks':
0.38; 'list': 0.39; 'use': 0.39; 'included': 0.61; 'initial':
0.61; 'likely': 0.61; 'skip:h 10': 0.61; 'above': 0.62; 'skip:\xc2
10': 0.62; 'lower': 0.62; 'here': 0.62; 'copy': 0.63;
'received:51': 0.64; 'top': 0.65; 'similar': 0.65; 'now,': 0.67;
'types': 0.67; 'back': 0.67; 'outside': 0.67; 'time,': 0.67;
'that,': 0.67; 'items': 0.68; 'matter': 0.68; 'and,': 0.69;
'following:': 0.69; 'relate': 0.69; 'within': 0.69; 'carry': 0.71;
'global': 0.73; 'little': 0.73; 'relevant': 0.73; 'name,': 0.75;
'skip:f 20': 0.75; '8bit%:100': 0.76; 'successful': 0.78;
'moment': 0.81; 'extra': 0.84; 'bid': 0.84; 'executing': 0.84;
'experiment': 0.84; 'forgotten': 0.84; 'honestly,': 0.84;
'occurring': 0.84; 'referenced': 0.84; 'referral': 0.84; 'scope':
0.84; 'subject: \n ': 0.84; 'down,': 0.91
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 2721F58AC
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1709663025;
bh=qFVKltwha+8SJNjoOZL84+IK4Ea/Q/cDU96AoZTF+jc=;
h=Date:From:Subject:To:References:In-Reply-To:From;
b=iwqka6ZQgOeyRKqAUvlARvghjQo25Cqb0lBkrxhd7geCO0IUMj89/yF6AxugdSybn
pxkjimJtD0VSuPI+posr7R232IoA03cezbSMFu01vPs302isYnU72xrYtDViCqxjeg
KeYNSIkfzkAN+T1/kF65rGak53nNELY271h9jhVrtz9HreQDFXiPfi12XYcNqkVEaU
ITBTw9kuDy8ba7buwOU8fZnjSyfHkjok+2stFxWcenJttS2Hz6y8D0m0rchlxadJQh
tEFxNhwsVxTWa4n/XUVXgdYBgt8vqlWcYmoVEGUCBlElS3N1QJsEdqVWHVBz393bBK
Sr9dQ02PAcu8g==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0ccad7a9-eaba-48e6-b972-d89e5a930c11@DancesWithMice.info>
X-Mailman-Original-References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
 by: dn - Tue, 5 Mar 2024 18:23 UTC

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..."
>
>

--
Regards,
=dn

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor