Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

IOT trap -- core dumped


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 overridJacob Kruger

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

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

  copy mid

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

  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: Re: Variable scope inside and outside functions - global statement
being overridden by assignation unless preceded by reference
Date: Wed, 6 Mar 2024 18:40:40 +0200
Lines: 100
Message-ID: <mailman.49.1709743250.3452.python-list@python.org>
References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<0ccad7a9-eaba-48e6-b972-d89e5a930c11@DancesWithMice.info>
<db322a1b-2d29-4b67-9d5c-3e8d8737c0f5@gmail.com>
<b0b253c4-5e9e-44a3-975a-11851009d52c@wichmann.us>
<cbaf27c2-d2cb-4de1-a1c0-02a9fc9bf252@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 g0y6boZHVS4ub/42VfSCvgrEVPMH+lJYblOwYWDO7g8A==
Cancel-Lock: sha1:cWSRIvnI1RqZ9s2uHyLkro3WAEA= sha256:7w63TKvI2mwqksjxQzBYUwW0UxP28q+llQGOuLnW/Rs=
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=KY1wE7QN;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.023
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'def': 0.04; 'chances':
0.05; 'variable': 0.05; 'telling': 0.07; 'child': 0.09;
'datetime': 0.09; 'objects,': 0.09; 'ok,': 0.09; 'prints': 0.09;
'values.': 0.09; 'import': 0.15; 'expected.': 0.16; 'importing':
0.16; 'intent': 0.16; 'interpreter': 0.16; 'mats': 0.16;
'object,': 0.16; 'purely': 0.16; 'received:mail-
ed1-x52f.google.com': 0.16; 'removes': 0.16; 'results,': 0.16;
'static': 0.16; 'subject:being': 0.16; 'subject:reference': 0.16;
'typing': 0.16; 'value?': 0.16; 'variable,': 0.16; 'wichmann':
0.16; 'wrote:': 0.16; 'python': 0.16; 'values': 0.17; 'message-
id:@gmail.com': 0.18; 'to:addr:python-list': 0.20; 'maybe': 0.22;
'version': 0.23; 'code': 0.23; 'command': 0.23; 'seems': 0.26;
'bit': 0.27; 'function': 0.27; '>>>': 0.28; 'sense': 0.28;
'example,': 0.28; 'it,': 0.29; 'header:User-Agent:1': 0.30;
'code,': 0.31; 'python-list': 0.32; 'specified': 0.32; 'words,':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'same': 0.34;
'printing': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'functions': 0.36; 'work,': 0.36; 'using': 0.37; "it's": 0.37;
'received:192.168': 0.37; 'file': 0.38; 'two': 0.39; 'adding':
0.39; 'list': 0.39; 'use': 0.39; 'explain': 0.40; 'both': 0.40;
'something': 0.40; 'in,': 0.60; 'including': 0.60; 'included':
0.61; 'skip:i 20': 0.62; 'clear': 0.64; 'about.': 0.64;
'updating': 0.64; 'your': 0.64; 'time.': 0.66; 'shows': 0.67;
'back': 0.67; 'outside': 0.67; 'exactly': 0.68; 'items': 0.68;
'revert': 0.68; 'and,': 0.69; 'readers': 0.69; 'types,': 0.69;
'you.': 0.71; 'claim': 0.71; 'ignore': 0.71; 'content': 0.72;
'global': 0.73; 'relevant': 0.73; 'skip:f 20': 0.75; 'returned':
0.81; 'scope': 0.84; 'skip:% 30': 0.84; 'subject: \n ': 0.84;
'skip:d 30': 0.86; 'and...': 0.91; 'me:': 0.91; 'retain': 0.91;
'retained': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1709743248; x=1710348048; darn=python.org;
h=content-transfer-encoding:in-reply-to:from:references:to
:content-language:subject:user-agent:mime-version:date:message-id
:from:to:cc:subject:date:message-id:reply-to;
bh=SApAqkyQ3EWxX3D5dRexGqbwLisD1W7Cq/i6a90PWmQ=;
b=KY1wE7QNEzsImeFW2aLAn+IG2dqtmtat4xmo8Mo4hQ55iDvf7Ok42/TkhCl4AYget+
GW6YpRaofGDmUMom/j77BfFKnDoIcMm68UUc6WTbZEShXQQCCrgDCmUJ+Cil4RBUUScR
WAs/UBq4gc8LH2+7F3RMsJLSwwOj9Sm796wT2enxx9XX5DQuTXYQDTV9nw2bzEjMEaJ+
UAWoF6xCkz5CDsQxvX76OlXkde7eDdjJFaR6w03ctvUQ+mcDKaLLBt+t+onWvwFskCpq
t6VYx+OiG0KeSetOIYF5GlgRT65Juas+dcl7jxGXY+0zqcreS2jKE/g7KEHdUfA4ctlo
60uA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1709743248; x=1710348048;
h=content-transfer-encoding:in-reply-to:from:references:to
:content-language:subject:user-agent:mime-version:date:message-id
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=SApAqkyQ3EWxX3D5dRexGqbwLisD1W7Cq/i6a90PWmQ=;
b=psTG6K0Rrqm82Ls2znwU6rhgC8tJBks1RufZ7fmxhj3wlLLMIN2ltLwf1fgpl8oiL1
RRbg312APlpanbBiAx3pUaBdJ5h9qOXAWsXcFgPt6+NX6dGwHd9Z1toFe70YQ55Ed9eL
Zyxjgz0WLEkBXVJv8sIeFdA9EYDuKqaEWRQFcYBtc9fILkCZ9xCMlhlQ1mSZAKuff+P9
gHCVTp8lr9PIVTTIsBP9PityIH0/xljhfL/DUgwZvLnU2cZOwdAaKz6B/ugh0k3EFzb9
DbVGGYLW9L7itKD6VmfO0/3Mr7/VYvKD5F30CIUWGkLVOEsaJDoeDL5MPZht5Cg6syAf
P0rA==
X-Gm-Message-State: AOJu0YzGFiqb75iCVuTmqGOPQYUV7W4bXhUojyWtA5HQBKbM8a05hII0
pBEtM2z+cE857wfJ5OBpuIl7RO5jXZAAv3/Y94F2kt1ZLHw0ny6WMk1qVl/b
X-Google-Smtp-Source: AGHT+IEbP4KtyAhx41xzoyj4v6u9FCuwqq8JVJSRcNpasEH3YQH73gvbhVDIQ7yaT3UNMp9lpNPSLg==
X-Received: by 2002:a17:906:d20a:b0:a3f:c006:f141 with SMTP id
w10-20020a170906d20a00b00a3fc006f141mr9888119ejz.62.1709743247617;
Wed, 06 Mar 2024 08:40:47 -0800 (PST)
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <b0b253c4-5e9e-44a3-975a-11851009d52c@wichmann.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: <cbaf27c2-d2cb-4de1-a1c0-02a9fc9bf252@gmail.com>
X-Mailman-Original-References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<0ccad7a9-eaba-48e6-b972-d89e5a930c11@DancesWithMice.info>
<db322a1b-2d29-4b67-9d5c-3e8d8737c0f5@gmail.com>
<b0b253c4-5e9e-44a3-975a-11851009d52c@wichmann.us>
 by: Jacob Kruger - Wed, 6 Mar 2024 16:40 UTC

Matt, other mail is more relevant - seems to maybe have more to do with
different behavour if import code, or not - no, does not make sense to
me - but, here's the command line contents including printing out id()
results, but, only working via importing code:

#---start session---

C:\temp\py_try>type scoping2.py
from 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():
    global dt_expiry
    dt_expiry = datetime.now()+timedelta(minutes=5)
    print("date value", dt_expiry.strftime("%Y-%m-%d %H:%M"))
    print("ID", id(dt_expiry))
# end of do_it function

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 *
>>> print(dt_expiry)
1970-01-01 00:00:00+00:00
>>> print(id(dt_expiry))
1808577867152
>>> do_it()
date value 2024-03-06 18:39
ID 1808572660736
>>> print(dt_expiry)
1970-01-01 00:00:00+00:00
>>> print(id(dt_expiry))
1808577867152
>>>
---end session---

As in, the two different ID values are being returned outside and inside
the function, whereas, if I included that bit inside the interpreter
while typing code manually, chances are the same ID would be retained
both inside and outside function.

Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."

On 2024/03/06 15:57, Mats Wichmann via Python-list wrote:
> On 3/6/24 05:55, 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:
>
>> 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
>
> No, it doesn't. Your code is working as one would expect. For example,
> adding prints for the l_test variable, and removing the .clear() which
> you claim makes it not work, shows me:
>
> before: l_test=[1, 2, 3], id(l_test)=140153285385856
> leaving do_it: l_test=[1, 2, 3, 1, 2, 3, 99], id(l_test)=140153285385856
> after: l_test=[1, 2, 3, 1, 2, 3, 99], id(l_test)=140153285385856
>
> It's the same list object, as you can see by the id values. And the
> list is updating as expected.
>
> And... you don't need the global statement for l_test. As it's
> mutable, you can mutate it in the function; the global only acts on
> assignment. Using "global" for that may make your intent more clear to
> readers though, although static checkers will grumble at you.
>
> You must be doing something additional that you're not telling us about.
>
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor