top | item 44584016

(no title)

tyrust | 7 months ago

I write a decent amount of Python, but find the walrus operator unintuitive. It's a little funky that API_KEY is available outside of the `if`, perhaps because I had first seen the walrus operator in golang, which restricts the scope to the block.

discuss

order

bobbylarrybobby|7 months ago

This isn't really unique to the walrus operator, it's just a general python quirk (albeit one I find incredibly annoying). `for i in range(5): ...` will leave `i` bound to 4 after the loop.

nomel|7 months ago

Oddly enough, "except" variables don't remain bound!

    try:
        x = int('cat')
    except Exception as e:
        pass
    print(e)  # <- NameError: name 'e' is not defined
So, it appears Python actually has three variable scopes (global, local, exception block)?

Alex3917|7 months ago

> `for i in range(5): ...` will leave `i` bound to 4 after the loop. reply

This "feature" was responsible for one of the worst security issues I've seen in my career. I love Python, but the scope leakage is a mess. (And yes, I know it's common in other languages, but that shouldn't excuse it.)

dec0dedab0de|7 months ago

I find it incredibly intuitive and useful that it does that. sometimes it drives me nuts that it doesn't do it for comprehensions but I can see why.

But if something fails in a loop running in the repl or jupyter I already have access to the variables.

If I want to do something with a loop of data that is roughly the same shape, I already have access to one of the the items at the end.

Short circuiting/breaking out of a loop early doesn't require an extra assignment.

I really can't see the downside.

tyrust|7 months ago

Oh yeah, that's a good point.

Python really is a bit of a mess haha.

all2|7 months ago

I cannot tell you how many times I've hit issues debugging and it was something like this. "You should know better" -- I know, I know, but I still snag on this occasionally.

pletnes|7 months ago

It would be utterly nuts otherwise. For loops over all elements in a sequence. If the sequence is a list of str, as an example, what would the «item after the last item» be?

yread|7 months ago

Maybe Python will get a let one day

MyOutfitIsVague|7 months ago

There's no block scope in Python. The smallest scope is function. Comprehension variables don't leak out, though, which causes some weird situations:

    >>> s = "abc"
    >>> [x:=y for y in s]
    ['a', 'b', 'c']
    
    >>> x
    'c'
    
    >>> y
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'y' is not defined
Comprehensions have their own local scope for their local variables, but the walrus operator reaches up to the innermost "assignable" scope.

slightwinder|7 months ago

This is just Pythons scoping, which is not restricted by block, but function. You have the same effect with every other element.

martin82|7 months ago

Wow. I had been writing Python for 15 years and I didn't even know that operator exists

chucksmash|7 months ago

It's only existed for 6 of those years so perhaps you can be forgiven :)

The last time I wrote Python in a job interview, one of the interviewers said "wait, I don't know Python very well but isn't this kinda an old style?" Yes, guilty. My Python dates me.