nonlocal fails ?

Chris Angelico rosuav at gmail.com
Thu Nov 14 13:53:50 EST 2019


On Fri, Nov 15, 2019 at 5:46 AM R.Wieser <address at not.available> wrote:
>
> Richard,
>
> > Assuming that one language works like another is a danger
>
> Abitrarily redefining words and using misnomers is another ...  ("global"
> and "nonlocal" respecivily if you wonder)

Every language that has a concept of "global" still has some sort of
limitation on it. If you look up that word in a dictionary, it won't
say "within the currently-running program" or anything. Yet, we
programmers are quite happy for global variables in one program to be
isolated from another - in fact, I think you'd be seriously
disconcerted if that were NOT the case. Is that "arbitrarily
redefining" the word global?

> > First, Python doesn't really have 'Variables' like a lot of other
> > languages
> > (they don't hold a bag of bytes), as Python names don't hold values, but
> > are just references to objects which actually hold the value
>
> That's a principle as old as programming languages itself I believe.

True, and it's also largely irrelevant here, so let's move on.

> > If the name is never bound to something, then the name will be also looked
> > for in the global namespace.
>
> Can you give an example of that ?    I currently cannot wrap my head around
> what you could be meaning there - anything I can imagine simply doesn't make
> any sense ...
>

def foo():
    x = 1
    print("x is", x)

Inside this function, you have one local name (x), and one name
reference that isn't local (print). When the function looks for print,
it looks in the globals, and then in the built-ins.

ChrisA


More information about the Python-list mailing list