kellysmith12.21
2021-2-12 11:05:26

What’s the issue with Python’s scope story? I haven’t used Python much, so I can’t draw many conclusions about it.


sorawee
2021-2-12 12:19:07

def f(b): if b: x = 1 print(x)


sorawee
2021-2-12 12:19:24

Does this cause a compile-time error? If not, what happens when you execute f(True) and f(False)?


kellysmith12.21
2021-2-12 13:44:07

Oh, that’s dreadful.


kellysmith12.21
2021-2-12 13:44:27

I would hope they Python folks went with the sane decision of making that a static error.


kellysmith12.21
2021-2-12 13:45:20

Then again, it’s my understanding that everything in Python is a dictionary, so it could be argued that it should be a dynamic error.


kellysmith12.21
2021-2-12 13:45:32

Either way, that’s awfully confusing.


laurent.orseau
2021-2-12 14:06:27

They should silently change def to be interpreted as d*e*f, that would be fun


popa.bogdanp
2021-2-12 14:26:42

f(True) prints 1 f(False) raises an UnboundLocalError


popa.bogdanp
2021-2-12 14:29:37

A related bug report I received recently: https://github.com/Bogdanp/dramatiq/issues/382


popa.bogdanp
2021-2-12 14:30:03

Shame on me for writing the code the way that I did. Shame on Python for letting me :sweat_smile:



samdphillips
2021-2-12 18:42:58

This is pretty technical, but the tldr is it’s not as simple as environments and lexical scoping.

https://cs.brown.edu/~sk/Publications/Papers/Published/pmmwplck-python-full-monty/


capfredf
2021-2-12 19:03:47

back in the days when I wrote a lot of python 2.x code, I had to always keep in mind some of python’s scoping issues.


sorawee
2021-2-12 19:04:28

Technically, Python almost has “lexical scope.”

The only place it doesn’t use lexical scope is at top-level. The behavior inside a function, on the other hand, is due to hoisting (in the same fashion as JS). Not lexical like Scheme, but lexical enough.


samdphillips
2021-2-12 19:14:59

Right. I was also thinking about how there isn’t implicit scope in places where a user (or just me?) may expect like conditional bodies.


sorawee
2021-2-12 19:17:17

Yeah. Hoisting is evil. To me, having lexical scope is not enough.


kellysmith12.21
2021-2-12 19:31:48

Hoisting?


sorawee
2021-2-12 19:43:18

In JS, this program:

function f(b) { if (b) { var x = 1; } console.log(x); } is similar to the Python program I posted earlier, but if b is false, it results in undefined.

The way it works is that the program is first transformed into:

function f(b) { var x = undefined; if (b) { x = 1; } console.log(x); } This transformation process is known as hoisting. It lifts variable declaration up.


sorawee
2021-2-12 19:44:15

Python is similar:

def f(b): if b: x = 1 print(x) is transformed to:

def f(b): x = <error if referenced> if b: x = 1 print(x)


kellysmith12.21
2021-2-12 21:23:29

That’s a confusing transformation to perform on a program.


sorawee
2021-2-12 21:28:13

Apparently it’s a beloved transformation for cool languages :slightly_smiling_face:


kellysmith12.21
2021-2-12 21:29:59

I’ll gladly stay in the uncool language camp, then :stuck_out_tongue: