Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Vax Vobiscum


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

SubjectAuthor
* Variable scope inside and outside functions - global statement being overridden Jacob Kruger
`- Re: Variable scope inside and outside functionsStefan Ram

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jacob.kruger.work@gmail.com (Jacob Kruger)
Newsgroups: comp.lang.python
Subject: Variable scope inside and outside functions - global statement being
overridden by assignation unless preceded by reference
Date: Tue, 5 Mar 2024 20:13:21 +0200
Lines: 139
Message-ID: <mailman.28.1709662411.3452.python-list@python.org>
References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ibIJ9qhtEwXjimhcJRgFywlVQnTOWmsipeXgODN88q5Q==
Cancel-Lock: sha1:TDR2Iyc+UdXojXd6KaxlEI13Udw= sha256:Q3FfjiYSLsju1dU5c0h54qwst8HtgrW81BvAY88XrMc=
Return-Path: <jacob.kruger.work@gmail.com>
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=gmail.com header.i=@gmail.com header.b=EmU6MvSW;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.034
X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'def': 0.04; 'variable':
0.05; 'moment,': 0.07; 'datetime': 0.09; 'itself,': 0.09; 'meant':
0.09; 'parse': 0.09; 'populate': 0.09; 'values.': 0.09; 'steps':
0.11; 'import': 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; 'not)': 0.16; 'psutil': 0.16; 'received
:mail-ej1-x635.google.com': 0.16; 'script.': 0.16; 'should,':
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; 'python': 0.16; 'values':
0.17; 'instead': 0.17; 'message-id:@gmail.com': 0.18; 'figure':
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; 'think':
0.32; 'else,': 0.32; 'empty': 0.32; 'requests,': 0.32; 'unless':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33;
'same': 0.34; 'mean': 0.34; 'received:google.com': 0.34; 'trying':
0.35; 'fine': 0.35; 'reflect': 0.35; 'yes,': 0.35;
'from:addr:gmail.com': 0.35; '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; 'still': 0.40; 'something': 0.40;
'try': 0.40; 'should': 0.40; 'reference': 0.60; '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;
'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; 'executing': 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-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1709662408; x=1710267208; darn=python.org;
h=content-transfer-encoding:subject:from:to:content-language
:user-agent:mime-version:date:message-id:from:to:cc:subject:date
:message-id:reply-to;
bh=BjkLPDtL1W/yVyXP5iF2ZqM4Xq83RXiTbAJO9seo9uE=;
b=EmU6MvSWM9Lo3dS09lYCr5rm0iaimTecnjRNGiAFXu0ov5FmQUVHmo0H1QtQ1Z8hVc
ToXNC2kZgax2rATuHj4/aWAHqVLNDvPEMM0FiQVh8Gsz4HKfVqErQZiITsOZZXrJ4wXN
k/zsUw/y3y4r+MHx2o83wVvDGEbuXW8IQppu/omZ1WacqW7xq3KGAj7tEmRrUtGJCsH+
iV7iyRC8r+S0f+gq6cUtYdn/WPKOO2HgnIzKlmQxmA0/NO3Ga0pwAJfzMGbAUc2A5V5/
TnrX+gblJD04lVksQZ63Hfc/yW8foCdt/XeXhbkLgxbDHnDPPPhDbrWmnrWPPsbT/ZDp
JF2g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1709662408; x=1710267208;
h=content-transfer-encoding:subject:from:to:content-language
:user-agent:mime-version:date:message-id:x-gm-message-state:from:to
:cc:subject:date:message-id:reply-to;
bh=BjkLPDtL1W/yVyXP5iF2ZqM4Xq83RXiTbAJO9seo9uE=;
b=uY+0GtVN+2l4wc+9UZlU5oQcu2wjIhPkd2YYC0JoxTbHALXFmSQXQnQbpuwT3zB7eU
4NEg2gWkiQJvS9SBW2ScCdddW/eEF2t4Bh71CSNJm2Fv5NYps4FrohZmfPcO3CUlC2VC
h464Y64VTTw41WvdY3qD4Z4h1gBV32PNRWNK+nC0Etr2BB+MHAhVegf3yw0sCiytytHH
bcHOwVQAE/KupTwDNcGnGRGayL/VGn5gihAJH1fEtoAz5bhL84azijPMNli+IVdk23JA
3LjWWAGn1v6hrmaCg1MB5lyvcdhXaA+FJ5xcNfxvZIg4tYb49Nkqwb5XQ1cQVbHPoeCL
JcxQ==
X-Gm-Message-State: AOJu0YzaFJEhs+HrKITHDKRj0dvz3BRH40qNZUbz/qo/L8FzKsvSJQFf
iUsd9rue4H2Wyeir6zEesiCiOWa95XsSJivE7LRHGK5IctQbWzQ/amwdateg
X-Google-Smtp-Source: AGHT+IEzz2t0RMYAy8WhztruoJzLNGSwWDkkLLxM+nF6PG1/iKhtTZ9027lOAGDZNNz5R8Zg/NWsUQ==
X-Received: by 2002:a17:906:693:b0:a45:26fd:f5ab with SMTP id
u19-20020a170906069300b00a4526fdf5abmr5491655ejb.23.1709662408239;
Tue, 05 Mar 2024 10:13:28 -0800 (PST)
User-Agent: Mozilla Thunderbird
Content-Language: en-US
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: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
 by: Jacob Kruger - Tue, 5 Mar 2024 18:13 UTC

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

Re: Variable scope inside and outside functions

<global-20240305194545@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Variable scope inside and outside functions
Date: 5 Mar 2024 18:46:31 GMT
Organization: Stefan Ram
Lines: 19
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <global-20240305194545@ram.dialup.fu-berlin.de>
References: <mailman.28.1709662411.3452.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de nCUIGylQ+CnBBxKg01k14Q/vFtsw6KckxZGy62qKTkV0Ip
Cancel-Lock: sha1:2hKRpiztogU/drhebX6DReaiDm8= sha256:cIjHekUOJEYstuxHJvdimDFEm3B5eMZL2WbzyYBMbVo=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Tue, 5 Mar 2024 18:46 UTC

Jacob Kruger <jacob.kruger.work@gmail.com> wrote or quoted:
>l_servers = []
....
>     global l_servers
>That should, in theory, mean that if I assign a value to that variable
>inside one of the functions, it should reflect globally?

Yes.

l =[ 1, 2, 3 ]

def f():
global l
l =[ 2, 3, 4 ]

f()

print( l )
# [2, 3, 4]

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor