why del is not a function or method?

Ned Batchelder ned at nedbatchelder.com
Mon Oct 16 12:36:52 EDT 2017


On 10/16/17 12:16 PM, Oren Ben-Kiki wrote:
> That doesn't explain why `del` isn't a method though. Intuitively,
> `my_dict.delete(some_key)` makes sense as a method. Of course, you could
> also make the same case for `len` being a method... and personally I think
> it would have been cleaner that way in both cases. But it is a minor issue,
> if at all.
>
> I guess the answer is a combination of "historical reasons" and "Guido's
> preferences"?

It would still need to be a statement to allow for:

     del x

since "x.del()" wouldn't affect the name x, it would affect the value x 
refers to.

--Ned.

>
>
> On Mon, Oct 16, 2017 at 6:58 PM, Stefan Ram <ram at zedat.fu-berlin.de> 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.
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>




More information about the Python-list mailing list