Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

In space, no one can hear you fart.


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 overridCameron Simpson

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs@cskk.id.au (Cameron Simpson)
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 08:20:00 +1100
Lines: 40
Message-ID: <mailman.30.1709674211.3452.python-list@python.org>
References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<ZeeMgL/4hFh/XDJz@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de hLUyH4BRRA8T0GFglOqjjgvw/LOWOaazne6GULTSEpJg==
Cancel-Lock: sha1:kHyVdhT99lKQelgxQVYVcOLTUDY= sha256:N9y1/vR4ikm34QLBULkyuYu8RpB1AxFWqxMsF7Drrx4=
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'def': 0.04;
'variable': 0.05; 'cheers,': 0.11; '(eg': 0.16; '_not_': 0.16;
'cameron': 0.16; 'definitions': 0.16; 'executed': 0.16;
'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16;
'from:name:cameron simpson': 0.16; 'manipulating': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'received:13.237': 0.16;
'received:13.237.201': 0.16; 'received:13.237.201.189': 0.16;
'received:cskk.id.au': 0.16; 'received:id.au': 0.16;
'received:mail.cskk.id.au': 0.16; 'simpson': 0.16; 'static': 0.16;
'subject:being': 0.16; 'subject:reference': 0.16; 'variable,':
0.16; 'variable.': 0.16; 'wrote:': 0.16; 'python': 0.16; 'name.':
0.19; 'to:addr:python-list': 0.20; 'issue': 0.21; 'code': 0.23;
'python,': 0.25; 'seems': 0.26; 'local': 0.27; 'function': 0.27;
'it,': 0.29; 'header:User-Agent:1': 0.30; "doesn't": 0.32;
'there': 0.33; 'particular': 0.33; 'header:In-Reply-To:1': 0.34;
'fine': 0.35; 'received:au': 0.35; 'change': 0.36; 'branch': 0.39;
'use': 0.39; 'decide': 0.39; 'try': 0.40; "there's": 0.61;
'received:13': 0.64; 'received:userid': 0.66; 'analysis': 0.69;
'[6,': 0.84; 'occurring': 0.84; 'referenced': 0.84; 'subject: \n
': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
User-Agent: Mutt/2.2.7 (2022-08-07)
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: <ZeeMgL/4hFh/XDJz@cskk.homeip.net>
X-Mailman-Original-References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
 by: Cameron Simpson - Tue, 5 Mar 2024 21:20 UTC

On 05Mar2024 20:13, Jacob Kruger <jacob.kruger.work@gmail.com> wrote:
>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).

You can use `global` or `nonlocal` to change where Python looks for a
particular name.

In the code below, `f1` has no local variables and `f2` has an `x` and
`l1` local variable.

x = 1
l1 = [1, 2, 3]

def f1():
print("f1 ...")
l1[1] = 5 # _not_ an assignment to "l1"
print("in f1, x =", x, "l1 =", l1)

def f2():
print("f2 ...")
x = 3
l1 = [6, 7, 9] # assignment to "l1"
print("in f2, x =", x, "l1 =", l1)

print("outside, x =", x, "l1 =", l1)
f1()
print("outside after f1, x =", x, "l1 =", l1)
f2()
print("outside after f2, x =", x, "l1 =", l1)

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor