Syntax for one-line "nonymous" functions in "declaration style"
Chris Angelico
rosuav at gmail.com
Tue Apr 2 13:15:27 EDT 2019
On Wed, Apr 3, 2019 at 3:55 AM Alexey Muranov <alexey.muranov at gmail.com> wrote:
> I clarified what i meant by an assignment, and i believe it to be a
> usual meaning.
>
> 1. `def` is not an assignment, there is no left-hand side or
> right-hand side. I was talking about the normal assignment by which
> anyone can bind any value to any variable.
Actually, a 'def' statement DOES perform assignment. It does a bit
more than that, but it definitely is assignment. You can easily check
the CPython disassembly:
>>> import dis
>>> def f():
... def g(x): return x * 3 + 1
...
>>> dis.dis(f)
2 0 LOAD_CONST 1 (<code object g at
0x7fa529daf540, file "<stdin>", line 2>)
2 LOAD_CONST 2 ('f.<locals>.g')
4 MAKE_FUNCTION 0
6 STORE_FAST 0 (g)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
[disassembly of g() omitted as it's irrelevant]
At run time, the statement "def g(x): ..." means "grab this code
object and this name, make a function, *and assign it to the name g*".
Your other points, I agree on.
ChrisA
More information about the Python-list
mailing list