Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Be consistent. -- Larry Wall in the perl man page


devel / comp.lang.python / Re: Scope confusion in Python REPL

SubjectAuthor
o Re: Scope confusion in Python REPLChris Angelico

1
Re: Scope confusion in Python REPL

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Scope confusion in Python REPL
Date: Thu, 13 Jan 2022 23:27:26 +1100
Lines: 55
Message-ID: <mailman.170.1642076859.3079.python-list@python.org>
References: <sm0y23kkkwu.fsf@lakka.kapsi.fi>
<CAPTjJmoFQuexLN1dyXwBqrb5RSUKZu3jFZtRPJ3sKvRKib1uPQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 5y4bWnCGH//1YpEd0GMs7gE+hWPX9tG2up5AE5V3QCSw==
Return-Path: <rosuav@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=cxtGTU4f;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'2022': 0.05; 'directly.': 0.07; 'matching': 0.07; 'module.':
0.09; 'other.': 0.09; 'prompt,': 0.09; 'prompt.': 0.09;
'something,': 0.09; 'subject:Python': 0.12; 'import': 0.15;
'"import': 0.16; 'because,': 0.16; 'chrisa': 0.16; 'context.':
0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'odd': 0.16; 'received:209.85.221.51': 0.16; 'received:mail-
wr1-f51.google.com': 0.16; 'work:': 0.16; 'wrote:': 0.16;
'python': 0.16; "can't": 0.17; 'thu,': 0.19; 'to:addr:python-
list': 0.20; 'basically': 0.22; 'ran': 0.22; "what's": 0.22;
"i'd": 0.24; 'anything': 0.25; 'examples': 0.25; 'saying': 0.25;
'seems': 0.26; 'else': 0.27; 'local': 0.27; 'function': 0.27;
'module': 0.31; 'putting': 0.31; '13,': 0.32; 'same,': 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'able': 0.34;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'bar': 0.35; 'from:addr:gmail.com': 0.35; 'functions': 0.36;
'main': 0.37; 'received:209.85': 0.37; 'received:209': 0.39;
'two': 0.39; 'use': 0.39; 'prompt': 0.39; 'still': 0.40; 'define':
0.40; 'whenever': 0.40; 'both': 0.40; 'want': 0.40; 'reference':
0.60; 'remember': 0.61; 'true': 0.63; 'everything': 0.63; 'back':
0.67; 'playing': 0.69; 'surrounding': 0.69; 'above,': 0.70;
'contexts.': 0.84; 'from.': 0.84; 'scope': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=lmS4Mnhv6nnKbN9yYvfDaljlk58rIJ2jFk9fTFKhOAU=;
b=cxtGTU4fhNs8hn1eZl9xnla/aWr7+K8zspYbc3lPNuGYqw8donQgbtyqC9yi6DcsTb
4yVIWjpf0JOLgiUtH+ey1YhXTWzEGuiK4AAUzWHn8ahF7GLAMHt3LMXhD9bgjygdnFYW
3DQG1/MIFMwYHmLATjP/L0nYSckqNjb4PRhNS/fmR1ZGR99SxTpApVIumzJeHvYRmKw7
TFqERRI+PXGAadUS0x5tPwj8lp6ztB4c7KEYvCDH0q/04xMjUmnNxvNu/YLBsWn4m9Jb
KWgFqteaZXDZVUTwSDR3q4/ZTk0B326IKAeJnbmWVawl2YVSVOmfyenrs8Y6lwJmPzaK
OfCA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=lmS4Mnhv6nnKbN9yYvfDaljlk58rIJ2jFk9fTFKhOAU=;
b=6iBKH4smAXiMR3XWmX6tV29utWKfLpMUKPgSDbRQpOWwaAIXTG3OWvJPpByglN2rPr
UYfd8Izug5xy3p+oyMVoRWOyUBEs0NDgMpMRJ9zyuNHeQbwPmEZEGVYQ5iccLR0m4F+v
gllQNd52iz2TkIpfUEu9a8gzXf38AScY84vh8qOkWO2ygggkOBTVGkFlr+0ODgjiGluR
/ar5pB1oBU9+fES9XZT6YvADGvHE/JYCqA3v9sYorLj+kGh7IZYOj6A2oyGtDJ8w9lQj
DUvrbUbnyvt6XcEB7+nx45nXvsLLqoM/cm6h/bEn606eeXDO7ZStTJISWSNmniSXvfXr
Q2Bg==
X-Gm-Message-State: AOAM530AvgZuv2SVUjdFfO9f/WAkb3aToUEe033LQTlVTMvuhRv5eGUj
Fti67IoY3Vq7jLOgbVV4fotZ6p5A/jcgnvFwYHf2gmGB
X-Google-Smtp-Source: ABdhPJxW1YXx5Kl5wEvyWHiGBEgW1dnGbC9WzitnRHRY/l8CiL2p7nlSfeFQCYO5nYX0L5JUWQgpHT+3Rr5XAFaNXrw=
X-Received: by 2002:adf:f14f:: with SMTP id y15mr3817408wro.564.1642076857184;
Thu, 13 Jan 2022 04:27:37 -0800 (PST)
In-Reply-To: <sm0y23kkkwu.fsf@lakka.kapsi.fi>
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: <CAPTjJmoFQuexLN1dyXwBqrb5RSUKZu3jFZtRPJ3sKvRKib1uPQ@mail.gmail.com>
X-Mailman-Original-References: <sm0y23kkkwu.fsf@lakka.kapsi.fi>
 by: Chris Angelico - Thu, 13 Jan 2022 12:27 UTC

On Thu, Jan 13, 2022 at 9:43 PM Anssi Saari <as@sci.fi> wrote:
>
>
> I ran into what seems odd scoping to me when playing with some matching
> examples for 3.10.
>
> I kinda thought that if I do from foo import * and from bar import * in
> the Python REPL, I'd get everything from foo and bar in the main
> scope. Or whatever the scope is at the prompt.

That is correct.

> And yet, if I define function foo in module foo and function bar in
> module bar and import as above, I can't call function bar from function
> foo. But if I define functions foo and bar at the prompt, then I can. So
> what's the difference in scoping here?

That's because, *inside function bar*, you don't have foo in scope.
Both functions are available at the REPL's module scope, but they both
exist in their own contexts.

When you define a function, it remembers its context. Inside that
function, you can refer to anything else in the function, or in any
surrounding function, or in the module. That's how closures work:

def make_adder(a):
def adder(b):
return a + b
return adder

Whenever you call make_adder, it will give you back a brand new
function, which will remember the value of 'a'.

The same is true of module-level names. If I have a function like this:

a = 42
def adder(b):
return a + b

and I import that function into some other module, it'll use 'a' from
its own module globals, not the module you're calling it from.

When you import something, all you're doing is getting a local
reference to it; "from foo import make_adder" is basically like saying
"import foo; make_adder = foo.make_adder". The function itself is
still the same, and it still remembers its original context.

Defining two functions at the prompt is like putting them in the same
file; they're able to refer to each other directly. What you may want
to do is have one module import the other one, which would then allow
them to refer to each other.

Hope that helps!

ChrisA


devel / comp.lang.python / Re: Scope confusion in Python REPL

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor