[Tutor] Variable reference
Steven D'Aprano
steve at pearwood.info
Tue Jul 7 04:07:44 CEST 2015
On Mon, Jul 06, 2015 at 05:18:16PM -0700, 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.
Not all variables are local variables, and del exists to manage more
than just name bindings. Deleting attributes and items from sequences
are good uses for it, as is deleting global names which are not needed.
You are right that del should not, generally, be used by beginners, and
especially not for manually managing names. Fortunately, beginners are
not usually inclined to write code like this:
def spam(s):
a = s.upper()
b = s + "s"
process(a, b)
return do_something_else(a, b)
del a, b, s
as that would be both pointless and silly. Not only is the del line
never reached by Python, but the local variables are automatically
deleted when the function returns, so it is a waste of programmer time
and effort to manually delete them.
--
Steve
More information about the Tutor
mailing list