Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

You see but you do not observe. Sir Arthur Conan Doyle, in "The Memoirs of Sherlock Holmes"


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 overridMats Wichmann

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: mats@wichmann.us (Mats Wichmann)
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 06:57:01 -0700
Lines: 48
Message-ID: <mailman.42.1709733432.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de wzf0GbgCqWYOpwcmGBwadg0T0tc4tiIvCQ5FqAtMpGVA==
Cancel-Lock: sha1:2ohQ2mApm6QMzTtrQKdcHv+u9Vc= sha256:YDGdpSzjaT6W5xdRxVkv3+KbqljSB0xXXHCBaqvxu64=
Return-Path: <mats@wichmann.us>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=pobox.com header.i=@pobox.com header.b=SaePq2R/;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.049
X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'variable': 0.05;
'telling': 0.07; 'child': 0.09; 'objects,': 0.09; 'ok,': 0.09;
'prints': 0.09; 'values.': 0.09; 'received:173': 0.13; 'import':
0.15; 'expected.': 0.16; 'intent': 0.16; 'object,': 0.16;
'purely': 0.16; 'removes': 0.16; 'static': 0.16; 'subject:being':
0.16; 'subject:reference': 0.16; 'value?': 0.16; 'variable,':
0.16; 'wrote:': 0.16; 'python': 0.16; 'values': 0.17; 'to:addr
:python-list': 0.20; 'version': 0.23; 'code': 0.23; 'seems': 0.26;
'function': 0.27; 'sense': 0.28; 'example,': 0.28; 'it,': 0.29;
'header:User-Agent:1': 0.30; 'python-list': 0.32; 'specified':
0.32; 'words,': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-Reply-
To:1': 0.34; '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;
'something': 0.40; '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; '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;
'scope': 0.84; 'subject: \n ': 0.84; 'and...': 0.91; 'me:': 0.91;
'retain': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=message-id
:date:mime-version:subject:to:references:from:in-reply-to
:content-type:content-transfer-encoding; s=sasl; bh=+e+WyxLNq/K/
XTgYmJQM/ifi3TKbOsVhBx2idm9ZWkA=; b=SaePq2R/b4Ut74kL4H89RZmS0Wry
vzR2yEBfx5ynbOp5YwM0xv201s2YCxEkwVtDXU9tZhpBNKQWw01OI/9sAcSGWW8G
YWsLXZLtw6n8hGXf11ie4lD2g81BDPzdTCoB2LEiGjRuJTfq8FMRevd/g1B9ylNi
SyWb5c2Xl1ul2Zo=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=wichmann.us;
h=message-id:date:mime-version:subject:to:references:from:in-reply-to:content-type:content-transfer-encoding;
s=2018-07.pbsmtp; bh=+e+WyxLNq/K/XTgYmJQM/ifi3TKbOsVhBx2idm9ZWkA=;
b=ExT661MQ3wmUBTLLuIXpnQ0hjl7D8RpHTleCoqlFlXGecwBqI+RslUa1ZEgUw6q4DLVH70xVTzvTiSXIsMxxlnuF+lT5mCq6OMtZmDqsQzq472ADmS9Fyj1nH3t41sUGLAFFQ2bXfMYfwYR+j1IZYAPGy89JfUvRXmKAZBxMO3s=
User-Agent: Mozilla Thunderbird
Content-Language: en-US
Autocrypt: addr=mats@wichmann.us; keydata=
xsDiBD9xp6oRBAC1vd3YI8Gcr1CxpV1gldNQu0uQsNaICDk+Ai3+R163s/P83JOYG+SBEA3P
v7iZx70qpQ3RzP7KrjF1Nm6j0em9ccUX2fPQUCAxXw5Hiq7CSMiwQQZRI6shcnyMh9XTKViT
WK5MrKDyvjDEn7epjKzKwPS5SG039l6XaOKU0A4uGwCgsNqUQqC0gMMcbKlJV8ql58iKmbMD
/ii8FPQrXmyS/FnsPs7UddV5qMHKm7NUH5oiKuMVyakInRyq9iIxuu3D4Ec6mWRKcGsjmIkW
HXCSz0aefs6dsqNqpU54cYioJ3wP5LzHK7oclgJPryVt5Qezbdutf8SQf8gVkaNIlkxwGUzi
bKTZ6CHzwlz9nNgeel0XPUcZzFxGA/4paeCg2rMSVuAhUQbsLYHu4XzTs9P16zaXkrtxc4m5
b+BF5xsLgTpyO5l859XudS2Gp+7/Y37dAU4QlyGGOboWmF1y9U5DnzBwG8ghsnym+ga58MJh
LdRdQQ6xQolCpEXOuzm40f2r5uMxF3KOJ7WpIPuGAkeCPru9BmlATH+zOs0gTWF0cyBXaWNo
bWFubiA8bWF0c0B3aWNobWFubi51cz7CYQQTEQIAIQIbAwYLCQgHAwIDFQIDAxYCAQIeAQIX
gAUCT0VyZwIZAQAKCRDAMaCQc9hUxiZBAJ9cWziGp7hVfsu5T+cQptc3rLNndQCgrZh8u5LW
BfJ5e/Y+3PwZ8UEm+ELOwE0EP5is8BAEAMtwzcA8TYf5UTjDMgwcSNoErTc9ag+IX05QFgL8
aF8sfJRv5atcitqQy0gSIsOzI+L/AFdPN/+QQI3dL1tCq14t32KPDtigDhzm6jVPXX5z+V9u
xnD8XTp+ZvNcWoHXjViM8aXeLLEiCpiVCho307h3XShvqoKINWRQWeAsKKDDAAMFA/48zaey
wiiEyvI0meJ1KkNHxdLP0yLODr1WV6j9xkPkLWOaIDw7dlwEOlF1N1YtZ2wa0p1wsttdIbIx
ffgwXmcH4zrdxUIMz3U0BqYzk5H+5cYFXECXTFVOmweS+JECYMj80PjRoKCO1eVO1N30zksB
36NnhZWPRWIhjK3ZarIYH8JGBBgRAgAGBQI/mKzwAAoJEMAxoJBz2FTG6VEAoKDYHfDp5Q3q
PuPvPahCE9HsXMgAAJ9INTqcLSJrOfyJ8q95nBO1T26H2Q==
In-Reply-To: <db322a1b-2d29-4b67-9d5c-3e8d8737c0f5@gmail.com>
X-Pobox-Relay-ID: 6959500A-DBC1-11EE-AF2C-A19503B9AAD1-81526775!pb-smtp21.pobox.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: <b0b253c4-5e9e-44a3-975a-11851009d52c@wichmann.us>
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>
 by: Mats Wichmann - Wed, 6 Mar 2024 13:57 UTC

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