Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

6 May, 2024: The networking issue during the past two days has been identified and fixed.


devel / comp.lang.python / Shadowing, was Re: learning python ...

SubjectAuthor
o Shadowing, was Re: learning python ...Peter Otten

1
Shadowing, was Re: learning python ...

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: __pete...@web.de (Peter Otten)
Newsgroups: comp.lang.python
Subject: Shadowing, was Re: learning python ...
Date: Tue, 25 May 2021 11:46:31 +0200
Lines: 57
Message-ID: <mailman.330.1621935999.3087.python-list@python.org>
References: <7dc017ca-c0a3-c66e-8b7c-ceea1c786c73@adminart.net>
<YKty/XyqeCBH9rcH@cskk.homeip.net>
<2ba5b89e-df8c-df0f-5a18-57852aa3a8bb@adminart.net>
<CAPTjJmok2XTPEkfREV=6mxTQqH=4LRqc+tYVL81FJJmXYgHmvQ@mail.gmail.com>
<9121937f-6364-a309-dbe7-3fd0ee117aa0@gmail.com>
<CAPTjJmqRe=B7v88hT+KNfRBD05nfO032RRt2MhF5QuNvyd9KhA@mail.gmail.com>
<e2842238-03f7-6a3f-1b6a-f8ec3b79a1af@adminart.net>
<s8ih1m$115s$1@ciao.gmane.io>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de h8YT74Jnx/qNMpZrYH22Iw/SchOtw6PzW4NppO8t5Tig==
Return-Path: <python-python-list@m.gmane-mx.org>
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; 'variable': 0.05;
'subject:python': 0.06; 'float': 0.07; 'mechanism': 0.07;
'modules': 0.07; 'fails': 0.09; 'int': 0.09; 'lookup': 0.09;
'message-id:@ciao.gmane.io': 0.09; 'received:ciao.gmane.io': 0.09;
'received:gmane.io': 0.09; 'received:list': 0.09; 'import': 0.14;
'problem': 0.15; 'anything,': 0.16; 'builtin': 0.16;
'from:addr:web.de': 0.16; 'importing': 0.16; 'into,': 0.16; 're-
define': 0.16; 'received:116.202': 0.16; 'received:116.202.254':
0.16; 'received:116.202.254.214': 0.16; 'respective': 0.16;
'wrote:': 0.16; 'name.': 0.20; 'exception': 0.23; 'ran': 0.23;
'to:addr:python-list': 0.23; 'run': 0.24; 'written': 0.24; '>>>':
0.26; 'old': 0.28; 'module': 0.28; 'header:User-Agent:1': 0.31;
'think': 0.31; 'but': 0.31; 'looked': 0.31; 'using': 0.33; 'header
:In-Reply-To:1': 0.33; 'setting': 0.34; 'same': 0.34; "we're":
0.35; 'two': 0.37; "it's": 0.38; 'list': 0.39; 'could': 0.40;
"skip:' 10": 0.40; 'simply': 0.62; 'skip:b 10': 0.62; 'your':
0.64; 'talking': 0.65; 'visible': 0.69; 'del': 0.71;
'received:116': 0.71; 'global': 0.72; 'easy': 0.75;
'accordingly.': 0.77; 'roles': 0.81; 'again:': 0.84; 'attribute':
0.84; 'thing?': 0.84
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101
Thunderbird/78.10.2
In-Reply-To: <e2842238-03f7-6a3f-1b6a-f8ec3b79a1af@adminart.net>
Content-Language: en-US
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <s8ih1m$115s$1@ciao.gmane.io>
X-Mailman-Original-References: <7dc017ca-c0a3-c66e-8b7c-ceea1c786c73@adminart.net>
<YKty/XyqeCBH9rcH@cskk.homeip.net>
<2ba5b89e-df8c-df0f-5a18-57852aa3a8bb@adminart.net>
<CAPTjJmok2XTPEkfREV=6mxTQqH=4LRqc+tYVL81FJJmXYgHmvQ@mail.gmail.com>
<9121937f-6364-a309-dbe7-3fd0ee117aa0@gmail.com>
<CAPTjJmqRe=B7v88hT+KNfRBD05nfO032RRt2MhF5QuNvyd9KhA@mail.gmail.com>
<e2842238-03f7-6a3f-1b6a-f8ec3b79a1af@adminart.net>
 by: Peter Otten - Tue, 25 May 2021 09:46 UTC

On 25/05/2021 05:20, hw wrote:

>> We're talking about many different things. If it's simply "num = ..."
>> followed by "num = ...", then it's not a new variable or anything,
>> it's simply rebinding the same name. But when you do "int = ...", it's
>> shadowing the builtin name.
>
> Why?  And how is that "shadowing"?
>
> What if I wanted to re-define the built-in thing?

When you write

foo

in a module the name "foo" is looked up in the module's global
namespace. If it's not found it is looked up in builtins. If that lookup
fails a NameError exception is raised.

>>> import builtins
>>> builtins.foo = "built-in foo"
>>> foo
'built-in foo'
>>> foo = "module-global-foo" # at this point builtins.foo is shadowed
>>> foo
'module-global-foo'
>>> del foo # delete the global to make the built-in visible again:
>>> foo
'built-in foo'

That mechanism allows newbies who don't know the builtins to write

list = [1, 2, 3]

without affecting other modules they may use. It also allows old scripts
that were written when a builtin name did not yet exist to run without
error.

The problem you ran into, using a name in two roles

float = float("1.2")

could be circumvented by writing

float = builtins.float("1.2")

but most of the time it is more convenient to think of builtins as names
that are predefined in your current module and act accordingly.

As you see redefining a builtin is as easy as importing builtins and
setting the respective attribute

>>> builtins.float = int
>>> float(1.23)
1

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor