learning python ...

Chris Angelico rosuav at gmail.com
Mon May 24 10:37:18 EDT 2021


On Tue, May 25, 2021 at 12:31 AM Michael Torrie <torriem at gmail.com> wrote:
>
> On 5/24/21 8:24 AM, Chris Angelico wrote:
> > On Tue, May 25, 2021 at 12:18 AM hw <hw at adminart.net> wrote:
> >> There are more alternatives:  Python might create a new variable with
> >> the same name and forget about the old one.  Or it doesn't forget about
> >> the old one and the old one becomes inaccessible (unless you have a
> >> reference to it, if there is such a thing in python).  How do you call that?
> >
> > It's the latter option: create a new variable, and the old one becomes
> > inaccessible. That's called "shadowing". It's how scoping works in
> > most languages (called "lexical scope").
>
> Is it really shadowing, though?  The old one is not only inaccessible,
> it's possibly reaped by the garbage collector, no?  Both nums are in the
> same scope so the one overwrote the other in the name table.  Or am I
> missing something.
>

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.

But the rules are very close to the same in most modern high level
languages. There are some small differences (eg Python doesn't have
sub-function scope, but C-like languages can do that; JavaScript kinda
does and kinda doesn't, because JavaScript), but by and large, the
same rules apply.

ChrisA


More information about the Python-list mailing list