Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The universe is all a spin-off of the Big Bang.


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 overridGrant Edwards

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!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: Wed, 6 Mar 2024 20:55:40 -0500 (EST)
Lines: 60
Message-ID: <mailman.57.1709776541.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>
<2248adf8-551f-4e86-8de8-be892d5978ed@tompassin.net>
<9c71abb0-b28d-4646-b130-9a4fdd529428@gmail.com>
<da56d0f1-ae1c-4f73-9493-c07f7c6d13c5@DancesWithMice.info>
<4Tqsp41DHBznTq9@mail.python.org>
X-Trace: news.uni-berlin.de ZmZBjEDfSWLIesSbDTys0wJL0pR+AUKz7JLyEFTbIBsw==
Cancel-Lock: sha1:zlGyhYykonU/6cSkJ+DyW+rr3kI= sha256:eZKABMDPXHlurE5SMvZfekViM0WQtlVbK+jb6S9tQOs=
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.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'entering':
0.05; 'hitting': 0.07; 'example:': 0.09; 'import': 0.15;
"'import": 0.16; "'python": 0.16; '(surely': 0.16;
'from:addr:grant.b.edwards': 0.16; 'from:name:grant edwards':
0.16; 'importing': 0.16; 'print(x)': 0.16; 'subject:being': 0.16;
'subject:reference': 0.16; 'wrote:': 0.16; 'python': 0.16;
'to:addr:python-list': 0.20; 'way.': 0.22; 'code': 0.23; 'run':
0.23; 'idea': 0.24; 'behavior': 0.26; '>>>': 0.28; 'header:User-
Agent:1': 0.30; 'module': 0.31; 'wondering': 0.31; "doesn't":
0.32; 'python-list': 0.32; 'skip:- 60': 0.32; 'unless': 0.32;
'but': 0.32; 'script': 0.33; 'trying': 0.35; 'usual': 0.35;
'from:addr:gmail.com': 0.35; "it's": 0.37; "we've": 0.39; 'use':
0.39; 'ide': 0.40; 'want': 0.40; 'method': 0.61; 'skip:b 10':
0.63; 'let': 0.66; 'message-id:invalid': 0.68; 'right': 0.68;
'important.': 0.69; 'manually': 0.69; 'global': 0.73; 'unusual':
0.81; 'experiment': 0.84; 'say,': 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: <4Tqsp41DHBznTq9@mail.python.org>
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>
<2248adf8-551f-4e86-8de8-be892d5978ed@tompassin.net>
<9c71abb0-b28d-4646-b130-9a4fdd529428@gmail.com>
<da56d0f1-ae1c-4f73-9493-c07f7c6d13c5@DancesWithMice.info>
 by: Grant Edwards - Thu, 7 Mar 2024 01:55 UTC

On 2024-03-07, dn via Python-list <python-list@python.org> wrote:

> The idea of importing a module into the REPL and then (repeatedly)
> manually entering the code to set-up and execute is unusual (surely type
> such into a script (once), and run that (repeatedly). As you say, most
> of us would be working from an IDE and hitting 'Run'. Am wondering why
> you weren't - but it's not important.

Unless the code is intended to be used as a module, 'import'ing it into
the REPL doesn't make sense.

A simple example:

---------------------------testit.py------------------------------
x = 'x'
y = 'y'
def foo():
global y
print("hi")
x = 'X'
y = 'Y'
print(x)
print(y)
------------------------------------------------------------------

The usual method to play with that interactively is

$ python -i testit.py
>>> x
'x'
>>> y
'y'
>>> foo()
hi
X
Y
>>> x
'x'
>>> y
'Y'
>>>

As we've seen, doing a 'from testit.py import *' doesn't let you test
what the OP was trying to test. Doing 'import testit.py' gets you
closer, but it's a hassle to test code that way. The right thing to do
is 'python -i <filename>' (or the equivalent button/option in an IDE).

https://docs.python.org/3/tutorial/interpreter.html

If you intended to use testit.py as a module, and wanted to experiment
with its behavior as a module, then go ahead and import it. But, don't
do 'from testit.py import *' until

1. you know how that differs from 'import testit.py'

and

2. you want to use that difference

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor