Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

It is much easier to suggest solutions when you know nothing about the problem.


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

SubjectAuthor
* Re: Variable scope inside and outside functions - global statement being overridGrant Edwards
`- Re: Variable scope inside and outside functionsStefan Ram

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b.edwards@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: Variable scope inside and outside functions - global statement
being overridden by assignation unless preceded by reference
Date: Tue, 5 Mar 2024 17:46:32 -0500 (EST)
Lines: 49
Message-ID: <mailman.31.1709678793.3452.python-list@python.org>
References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<ZeeMgL/4hFh/XDJz@cskk.homeip.net>
<4Tq9fJ0qN7znVFY@mail.python.org>
X-Trace: news.uni-berlin.de xjEKcgrMnJXRa9mzQxmmVgHAR27uk3Od1X4BAQ+rFOZQ==
Cancel-Lock: sha1:vsSj8m8o4/UweHKeMxJtDwoEAS4= sha256:vbIgrBE/ZGcEGsED8VOMPpupVIB51lWw/pqDrYzxiDs=
Return-Path: <grant.b.edwards@gmail.com>
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.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'searching':
0.05; 'variable': 0.05; 'url-ip:65/8': 0.07; 'describe': 0.09;
'example:': 0.09; 'paragraph': 0.09; 'say:': 0.09; '(eg': 0.16;
'cameron': 0.16; 'declared': 0.16; 'definitions': 0.16; 'equally':
0.16; 'executed': 0.16; 'from:addr:grant.b.edwards': 0.16;
'from:name:grant edwards': 0.16; 'question,': 0.16; 'simpson':
0.16; 'static': 0.16; 'subject:being': 0.16; 'subject:reference':
0.16; 'url-ip:65.9/16': 0.16; 'variable,': 0.16; 'variable.':
0.16; 'wrote:': 0.16; 'python': 0.16; 'reached': 0.17; 'says':
0.17; 'to:addr:python-list': 0.20; 'exception': 0.22; 'python,':
0.25; "isn't": 0.27; 'local': 0.27; 'function': 0.27; 'goes':
0.28; 'header:User-Agent:1': 0.30; "doesn't": 0.32; 'python-list':
0.32; 'words,': 0.32; 'unless': 0.32; 'there': 0.33;
'from:addr:gmail.com': 0.35; 'those': 0.36; "it's": 0.37;
'thanks': 0.38; 'branch': 0.39; 'decide': 0.39; "there's": 0.61;
'above': 0.62; 'limited': 0.62; 'simply': 0.63; 'message-
id:invalid': 0.68; 'matter': 0.68; 'only.': 0.69; 'analysis':
0.69; 'sites': 0.70; 'global': 0.73; 'scope': 0.84; 'subject: \n
': 0.84
User-Agent: slrn/1.0.3 (Linux)
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: <4Tq9fJ0qN7znVFY@mail.python.org>
X-Mailman-Original-References: <aff560df-2f57-47d9-ad81-74c21960c21d@gmail.com>
<ZeeMgL/4hFh/XDJz@cskk.homeip.net>
 by: Grant Edwards - Tue, 5 Mar 2024 22:46 UTC

On 2024-03-05, Cameron Simpson via Python-list <python-list@python.org> wrote:

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

Unfortunately, crap "information" sites like geeksforgeeks often
describe this either incorrectly or so vaguely as to be worthless.
>From the page https://www.geeksforgeeks.org/global-local-variables-python/

Python Global variables are those which are not defined inside
any function and have a global scope whereas Python local
variables are those which are defined inside a function and their
scope is limited to that function only.

Since "define" (in this context) isn't a term of art in Python, and
it's never defined on the page in question, the quoted paragraph is
not meaningful: it simply says that "global variables are global and
local variables are local".

That page goes on to say:

In other words, we can say that local variables are accessible
only inside the function in which it was initialized

This is equally crap. It doesn't matter whether the variable is
initialized or not. As Cameron correctly stated, if a function
contains an assignment to a variable, and that variable is not
declared global, then that variable is local. For example:

def foo():
print(s)
if 0:
s = "there"
print(s)

In the function above s _is_not_ initialized in the function foo().
However, foo() does contain an assignment to s, therefore s is local
unless declared global/nonlocal. [And the first print() will throw an
exception even if there is a value bound to the global name 's'.]

Unfortunately (presumably thanks to SEO) the enshittification of
Google has reached the point where searching for info on things like
Python name scope, the first page of links are to worthless sites like
geeksforgeeks.

Re: Variable scope inside and outside functions

<define-20240306000929@ram.dialup.fu-berlin.de>

  copy mid

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

  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 23:12:24 GMT
Organization: Stefan Ram
Lines: 17
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <define-20240306000929@ram.dialup.fu-berlin.de>
References: <mailman.31.1709678793.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 b/RyqiDSF/epraV9Wl1R9A7k1AyV5UKNmHhXQHQsJ24rPK
Cancel-Lock: sha1:cMY6v48bTntrKUe9wYvYAJkORhk= sha256:VRt+Y266D5LXBjRkbSRAFFTz6XUdpLOPQ6rNMnECmM0=
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 23:12 UTC

Grant Edwards <grant.b.edwards@gmail.com> wrote or quoted:
> Python Global variables are those which are not defined inside
> any function and have a global scope whereas Python local
> variables are those which are defined inside a function and their
> scope is limited to that function only.
>Since "define" (in this context) isn't a term of art in Python, and

|If a local variable is defined in a block,
The Python Language Reference, Release 3.13.0a0,
4.2.2 Resolution of names, p1

|checking the module’s namespace for a variable named __all__;
|if defined, it must be a sequence of strings
ibid., 7.11

|class variable A variable defined in a class
ibid., Appendix A

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor