why del is not a function or method?

Chris Angelico rosuav at gmail.com
Mon Oct 16 12:24:30 EDT 2017


On Tue, Oct 17, 2017 at 3:07 AM, bartc <bc at freeuk.com> 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.)

team.pop(2) will in-place delete one element. So you have both options.

ChrisA



More information about the Python-list mailing list