Steven D'Aprano wrote: > 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
Read again. I said that del x in the global namespace can be emulated with globals().pop("x") and that there is no equivalent to del x if x is a local/nonlocal name. > > We can work around this by quoting the variable name: > > delete("x") # this could work > > but that is not an elegant design. I agree that if you think that explicitly unbinding a name is a useful feature of the language a statement is the way to go. For me it's a feature I hardly ever use and that I hardly ever find compelling in other people's code. I'd happily resort to x = None should the need arise to dereference a specific variable. > 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. I think Danny's point was that you should not micromanage name bindings at all. Then Alan added that del is useful for dicts etc. on which I replied that a method would be sufficient for that. > 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. I'd put that the other way round: allowing both del a["x"] # invokes a method on `a` and del x # manipulates a namespace glosses over the "conceptual difference"; if a clean approach were the goal only the latter should be allowed. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor