[Tutor] Variable reference

Steven D'Aprano steve at pearwood.info
Tue Jul 7 17:45:34 CEST 2015


On Tue, Jul 07, 2015 at 04:08:30PM +0200, Peter Otten wrote:

> For dicts and lists a method would work as well. Even now you can write
> 
> items.pop(index) # instead of del items[index]
> lookup.pop(key) # del lookup[key]
> 
> If you find the name pop() random or hard to discover a delete() method 
> could be added.
> 
> globals().pop("name") # instead of del name in the global namespace
> delattr(obj, "name") # del obj.name
> 
> For the above the replacement is less elegant, but I don't think it matters 
> for a rarely used feature. So for everything but local and nonlocal names 
> del is syntactic sugar at best.

Not so. The point of del being a statement is that it should be 
considered an operation on the *reference*, not the *value* of the 
reference. So:

x = 23
delete(x)  # if it existed, it would see the value 23
del x  # operates on the reference "x", not 23

We can work around this by quoting the variable name:

delete("x")  # this could work

but that is not an elegant design. It's a work-around for the fact that 
Python doesn't have dedicated syntax to say "operate on the reference 
foo" rather than the value of foo.

In Python, I think there are only two operations on references 
themselves: binding, and unbinding. For some purposes, we can consider 
unbinding just a special case of binding. (For example, adding a `del 
spam` line to a function makes spam a local, just as assigning to it 
would.) All binding operations are firstly statements, not function 
calls:

x = 23  # not assign("x", 23)
import spam
for eggs in sequence
with expr as cheese
except SomeError as foo


and del is no exception. For some of these, there are functional 
versions: setattr, delattr come to mind, but I don't think there are 
many others. dict.pop and similiar are not conceptually the same, as 
they don't operate on references, they operate on keys, indexes, names 
as strings, etc.

I acknowledge that there is some overlap between the two, and one can 
replace the other (at least sometimes), but conceptually they operate in 
different spheres.



-- 
Steve


More information about the Tutor mailing list