why del is not a function or method?

Peter Otten __peter__ at web.de
Mon Oct 16 12:32:15 EDT 2017


bartc wrote:

> On 16/10/2017 16:58, Stefan Ram wrote:
>> Xue Feng <xf.lotus at yahoo.com> writes:
>>> I wonder why 'del' is not a function or method.
>> 
>>    Assume,
>> 
>> x = 2.
>> 
>>    When a function »f« is called with the argument »x«,
>>    this is written as
>> 
>> f( x )
>> 
>>    . The function never gets to see the name »x«, just
>>    its boundee (value) »2«. So, it cannot delete the
>>    name »x«.
>> 
>>    Also, the function has no access to the scope of »x«,
>>    and even more so, it cannot make any changes in it.
>> 
>>    Therefore, even a call such as
>> 
>> f( 'x' )
>> 
>>    will not help much.
> 
> What about del team[2]?
> 
> There is no name involved here, and even a reference to team[2] won't
> help.
> 
> Presumably there is no other way to do an in-place deletion of an
> element of a list. (Inserting an element is different.)

There is another way:

team.pop(2)

Stefan's explanation may work for

del x

if you discard

x = None  # get rid of the huge object that x was bound to before

as a hack, but not for

del x[y]

or

del x.y





More information about the Python-list mailing list