why del is not a function or method?
Terry Reedy
tjreedy at udel.edu
Tue Oct 17 11:44:43 EDT 2017
On 10/17/2017 1:07 AM, Steve D'Aprano wrote:
> The point is, if del were a function, then calling del(x) would pass the
> *value* of x into the function, not the name 'x'
So we would have to quote either the entire rest of the statement, or
each item separately,
> -- unless the interpreter
made del a special case, not a regular function.
This is what Lisp does. Most functions are 'normal': unquoted argument
expressions are evaluated. Some are 'special': at least one of the
argument expressions is automatically quoted or treated specially is
some way. Users have to memorize which functions are special and which
arguments are automatically quoted. Learning the exceptions and quoting
rules was for me a barrier to learning Lisp.
In Python, the 'special functions' of Lisp are statements. With print
now a function, I believe every python statement with arguments does
something special with at least one argument.
In assignments statements, for instance, everything to the right of the
final '=' is evaluated normally. Everything to the left of any '=' gets
special treatment. The final 'get object' part of expression evaluation
is replace by 'create association' with the corresponding object on the
right side.
In CPython left-hand expressions are not merely quoted for runtime
evaluation and use. They are parsed at compile time and compiled to
almost normal bytecode. The difference is that a load bytecode is
replace by a store bytecode (that takes a second argument).
>>> dis('a[b] = c[d]')
1 0 LOAD_NAME 0 (c)
2 LOAD_NAME 1 (d)
4 BINARY_SUBSCR
6 LOAD_NAME 2 (a)
8 LOAD_NAME 3 (b)
10 STORE_SUBSCR
12 LOAD_CONST 0 (None)
14 RETURN_VALUE
>>> dis('a(b).c = d(e).f')
1 0 LOAD_NAME 0 (d)
2 LOAD_NAME 1 (e)
4 CALL_FUNCTION 1
6 LOAD_ATTR 2 (f)
8 LOAD_NAME 3 (a)
10 LOAD_NAME 4 (b)
12 CALL_FUNCTION 1
14 STORE_ATTR 5 (c)
16 LOAD_CONST 0 (None)
18 RETURN_VALUE
For statements executed repeatedly, this is more efficient than quoting
for runtime evaluation.
> But if the interpreter did that, why bother with function notation and the
> extraneous parentheses? Why not just make it a statement and avoid surprising
> the programmer by giving the del function super-powers that no other function
> has?
Tcl does not use parentheses for function calls. But with only one
syntax, it still needs arcane quoting rules.
--
Terry Jan Reedy
More information about the Python-list
mailing list