why del is not a function or method?

Steve D'Aprano steve+python at pearwood.info
Mon Oct 16 22:35:40 EDT 2017


On Tue, 17 Oct 2017 03:55 am, Oren Ben-Kiki wrote:

> True... technically, "Deletion of a name removes the binding of that name
> from the local or global namespace". Using `x.del()` can't do that.

Indeed -- not without introducing magic special syntax into the parser, and
effectively treating ".del()" as a special postfix operator that takes the
name x given before it.

In other words, Python would have had introduce special parsing rules. Instead
of always using:

    <NAME> DOT <NAME> 

and perform an attribute access, Python would first have to check for the
special case:

    <NAME> DOT "del()"

and treat it differently. What if you wrote:

    x.method().del()

or

    x.del().method()

for example? Should that be treated as a syntax error? Special syntactic cases
make for difficult to understand parsers, and surprising syntax.


> That said, I would hazard to guess that `del x` is pretty rare (I have
> never felt the need for it myself).

Indeed. `del` is pretty rare. The main reason I use it is to delete temporary
variables in repetitious code. For example, in one of my modules I have
something close to this snippet to validate a post-condition on two global
dictionaries:


    for d in (dict1, dict2):
        for x in d.values():
            assert x.code == x._calculate_code()
    del d, x


d and x are temporary variables that exist only for the validation, and I
don't want them hanging around polluting the module namespace once I'm done
with them.


> Ruby doesn't even have an equivalent 
> operation, and doesn't seem to suffer as a result. If Python used methods
> instead of global functions for `len` and `del`, 

The case of `len` is irrelevant.


> and provided a `delete_local_variable('x')` for these rare cases, 

Don't forget delete_nonlocal_variable, delete_global_variable,
delete_class_variable, and delete_builtin_variable. And if Python ever added
any other scopes (say, a "process global" scope) then you'd need a new delete
function to delete from it.

In fact, deleting *local* variables is probably the least useful use of del
imaginable. Its a local variable -- nothing outside of the current function
can access it, so who cares about deleting it?

In fairness, you could possibly get away with a single function if, either,
you passed the namespace to delete from:

delete_variable('foo', globals())

or if it introduced some sort of implementation-specific magic to work out
which namespace the variable name was defined in. But that would require the
function to know the namespaces of the *caller*, not its own namespaces, and
there is no generally supported way to do that.

So in other words, it would require significant compiler magic. In general,
Python prefers to put the compiler magic in statements, not functions.



> that could have been a viable solution.

True. It would have been an ugly, inelegant solution, but it would be a
solution.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list