[Tutor] Variable reference
Peter Otten
__peter__ at web.de
Tue Jul 7 16:08:30 CEST 2015
Alan Gauld wrote:
> On 07/07/15 01:18, Danny Yoo wrote:
>> I'd also add that the 'del' statement has near-zero utility.
>>
>> 'del' is a language blemish. It should not be used by beginners,
>> because it asks them to try to manually manage the lifetime of their
>> variable names. That's an unreasonable and ridiculous burden.
>> Functions have local variables for a reason.
>
> I don't know that I'd go that far. There are valid uses for
> it in deleting things from dictionaries and the like.
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.
> But I agree its not needed very often and can lead to
> users over-managing their data.
More information about the Tutor
mailing list