Why is del(ete) a statement instead of a method?

Greg Ewing see_reply_address at something.invalid
Wed Oct 9 19:39:36 EDT 2002


Greg Brunet wrote:

> I agree for the need for a .del action
> (statement / function / method).  I just don't understand why it's not
> a method.


The "del" statement actually does three completely
different things:

(a) Removing an item from a sequence or mapping:

        del x[i]

(b) Removing an attribute from an object:

        del x.a

(c) Removing a name from the current namespace:

        del x

The compiler recognises each of these cases separately
and generates different bytecode for them. There's no
reason in principle why they should even have the same
syntax.

Operations (a) and (b) could just as well be done by
a method of x, and in fact some objects do have methods
that can be used to get the same effect.

However, operation (c) can't be done by any method of
the object to which x refers, because it doesn't operate
on that object at all. Rather, it operates on the
namespace holding the name "x". So, in that case, a
special statement is required.

Given the existence of that statement, Guido evidently
decided that it would be good if it could be used for
(a) and (b) as well, and made it so.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list