why del is not a function or method?
Steve D'Aprano
steve+python at pearwood.info
Mon Oct 16 20:53:27 EDT 2017
On Tue, 17 Oct 2017 03:16 am, Oren Ben-Kiki wrote:
> That doesn't explain why `del` isn't a method though.
`del` cannot be a method or a function, because the argument to `del` is the
name of the variable, not the contents of the variable.
If we write:
x = 123
del x
then `del` needs to delete the *name* "x", not the value of x, namely 123. If
del were a function or method, it would only see the value, 123, and have no
idea what the name is.
`del` is kind of like an "anti-assignment" in that the argument to `del` must
be exactly the same sort of expression that can appear on the left hand side
of assignment:
123 = 1+1 # illegal
del 123 # also illegal
--
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