←back to thread

317 points est | 2 comments | | HN request time: 0.478s | source
Show context
gbfowler ◴[] No.17448582[source]
It is not syntactic sugar, "x := 10" is an assignment expression in contrast with "x = 10", which is a statement.

Hence the former can be used in contexts like "if x := 10: pass", which is the whole point of the PEP.

replies(2): >>17448624 #>>17448651 #
Aardwolf ◴[] No.17448624[source]
Why is it not syntactic sugar? It looks like convenience. You could argue everything above machine language is syntactic sugar.
replies(5): >>17448721 #>>17448736 #>>17448847 #>>17448880 #>>17448984 #
akvadrako ◴[] No.17448721[source]
Not so. Many things change the semantics, otherwise high level languages would just be glorified macros.

For example, import mod is NOT defined as

  mod = eval(open("mod.py").read())
but involves abstract load module operation, which is dependant on the environment.

That's why := is just syntactic sugar; there are no new semantics.

replies(3): >>17448810 #>>17448892 #>>17450904 #
junke ◴[] No.17448810[source]
Honestly curious, how do you expand a "x := y" expression into an expression that returns y and affects x as a side-effect?
replies(2): >>17449244 #>>17451883 #
1. akvadrako ◴[] No.17449244[source]
I'm not sure exactly, but my point is that a program transformation can do it. However, no transformation can change an import to other operations while perserving semantics.

Just for fun, this seems to work:

  (locals().pop('x', None), locals().setdefault('x', y))[1]
replies(1): >>17449346 #
2. yorwba ◴[] No.17449346[source]
That only works at top-level scope, where locals() == globals(). Inside a function, changing the dict returned by locals() doesn't actually change the variable.