in reply to Re: Nesting Functions
in thread Nesting Functions
Local and enclosing are lexical myPython doesn't have lexicals. All variables are rather some equivalent of package variables, where each function counts as a package that can't access variables of other function. So a variable can be used as long as it as been defined sometime before in the function, regardless of scope:
I had some difficulty understanding the meaning and use of with statements, until I realized it was just a mechanism to simulate lexical scope (with the possibility to do some clean up on a variable at the end of a defined scope). You might find some example when looking for variable declaration in python, but actually the (new) feature is just type enforcing, there is no equivalent to my in python.>>> def scope(): ... for x in range(1,10): ... if x > 3: ... print(a) ... else: ... a=x ... >>> scope() 3 3 3 3 3 3 >>> print(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined
That's the thing I dislike the most about python, just above the lack of explicit block-ending tokens (curly brackets :P).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Nesting Functions
by LanX (Saint) on Jul 19, 2019 at 12:46 UTC | |
by Eily (Monsignor) on Jul 19, 2019 at 13:32 UTC | |
by LanX (Saint) on Jul 19, 2019 at 13:48 UTC | |
by choroba (Cardinal) on Jul 19, 2019 at 14:12 UTC | |
by LanX (Saint) on Jul 19, 2019 at 14:22 UTC | |
by Eily (Monsignor) on Jul 19, 2019 at 14:23 UTC | |
by hippo (Archbishop) on Jul 19, 2019 at 14:38 UTC | |
| |
by LanX (Saint) on Jul 19, 2019 at 14:39 UTC | |
| |
by LanX (Saint) on Jul 19, 2019 at 22:42 UTC | |
by LanX (Saint) on Jul 19, 2019 at 14:15 UTC |