Statements as expressions [was Re: Undefined behaviour in C]

Chris Angelico rosuav at gmail.com
Tue Mar 29 05:09:39 EDT 2016


On Tue, Mar 29, 2016 at 7:26 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> del x
>
> Should it return None? The string 'x'? How about the value that x had just
> before it was deleted? True if that allowed the value to be garbage
> collected, False if it wasn't? Or the other way around? None of these
> suggests feel either useful or obviously right.

Returning the value x had just before being deleted is certainly
useful - consider the case where x is not a simple name but a
subscripting, which makes this into a destructive lookup. Saying
whether the object was garbage collected or not could also be useful,
but feels very low level.

But if Python were to make 'del' into an expression, the only
semantics needed would be for simple names. For everything else, the
value of 'del x[y]' or 'del x.y' would simply be 'whatever
__delitem__/__delattr__ returns'. And even for simple names, the
semantics could be defined by the enclosing scope's dictionary. It'd
be like Python's operators; yes, you normally expect "x < y" to return
a truthy or falsy value, and for most built-in types it'll return
either True or False, but you can return anything at all - including a
lazy evaluation object for a DSL. Someone could, for instance, make
"del User[42]" return a Query object which, when executed, deletes the
user with primary key 42.

(Note: I am not advocating this. Just saying what would, IMO, be
consistent with the rest of Python.)

ChrisA



More information about the Python-list mailing list