11

Why did the creators of python prefer this syntax (instruction)

del list[index]

over this (method)?

list.del(index)

It seems to me that del belongs to the same "cathegory" as append, remove, find, e.t.c. and therefore should have the same syntax (be a method), but due to some reason the creators of python implemented it as an instruction. Why did they do it?

Ilya Peterov
  • 1,975
  • 1
  • 16
  • 33
  • 1
    because you can also del a class, a method, etc (for example in monkeypatching) – DevLounge Sep 24 '15 at 22:03
  • A) `del` has more uses than just removing items from a `list`. B) You can remove items from a `list` by name with the `remove()` method or by index with the `pop()` method. I recommend doing more research into relevant topics (e.g. `del`, `list`) in the future. – TigerhawkT3 Sep 24 '15 at 22:07
  • @TigerhawkT3 Yes, I forgot about `pop()`, my mistake. But why do they keep `del` if `pop()` is an an alternative for both lists and dictionaries? – Ilya Peterov Sep 24 '15 at 22:11
  • @Apero - Yeah, zero-research users upvoting each other is a pretty major problem for SO. Since upvotes are worth so much more rep than downvotes, it's easy for someone to "win" while SO loses. – TigerhawkT3 Sep 24 '15 at 22:20
  • 4
    @TigerhawkT3 The thing is, in most books and tutorials, `del` statement is presented as a method to delete entries from lists and dictionaries, so I didn't think it could have a more general use, that's why I asked the question. Though, it seems to me that the reasons why you are writing this are more about you than about my question. – Ilya Peterov Sep 24 '15 at 22:27
  • Does `del var_name` look like a method? – TigerhawkT3 Sep 24 '15 at 22:28
  • @TigerhawkT3 Shouldn't have used the word "method". The one I meant was "way". – Ilya Peterov Sep 24 '15 at 22:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90571/discussion-between-ilya-peterov-and-tigerhawkt3). – Ilya Peterov Sep 24 '15 at 22:31
  • No way is this duplicate of the linked question. This question is about why the syntax is what it is and not when it is useful. IMO it is a statement because it has different rules than object method calls. Without this we would have to write alist.del(alist[0::2]) for example. That would go against DRY and invite mistakes. alist[] cannot return a slice object that would refer to the list cause that would invite sneaky memory leaks if you were to store reference to the slice. del was special anyway to delete variables etc so they used that. Not sure I agree with that choice – nyholku Sep 04 '21 at 15:39

1 Answers1

2

Because del is a statement that you can delete several things with it, and since when you want to delete list_name[index] with del actually you want to delete an object and this is the job that del does for other objects so there is no need to create an redundant attribute for lists to does that!

Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints.

Deletion of a target list recursively deletes each target, from left to right.

Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. If the name is unbound, a NameError exception will be raised.

Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object).

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    Now I see, `del` can delete not only elements of lists or dictionaries, but any objects, so as long as elements of lists and dictionaries are objects, there is no point in adding more ways to delete an element. Thank you. – Ilya Peterov Sep 24 '15 at 22:16
  • @IlyaPeterov Yep indeed! and welcome! – Mazdak Sep 24 '15 at 22:17
  • 1
    Better question is, why is there a `del` statement instead of a `delete` built-in function? – Malik Brahimi Sep 24 '15 at 22:21
  • @IlyaPeterov - You would have found this basic documentation by simply Googling "python del statement". – TigerhawkT3 Sep 24 '15 at 22:23
  • @Kasramvd - Could you provide a link to the quoted documentation? – TigerhawkT3 Sep 24 '15 at 22:23
  • Never mind; there's a link to this documentation in my suggested dup question. – TigerhawkT3 Sep 24 '15 at 22:40
  • Hmm. Saying that it deletes from left to right implies to me that elements move left as each element is deleted and thus the remaining indexes no longer point to the elements they originally pointed to and what is obviously the intention. Deleting from right to left would make much more sense. So if the documation is correct, then there much more than meets the eyy in that "recursively deletes" sentence. – nyholku Sep 04 '21 at 10:57
  • @TigerhawkT3 "The basic documentation" does not really answer the question. My take on the "why" is in comment to the question. – nyholku Sep 04 '21 at 15:42
  • @nyholku - This answer mostly just quotes that documentation (without [citing it](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement), for some reason), so if that doesn't answer the question, I guess the only thing that can do so is an interview with the person who designed this language feature (probably GvR but not necessarily). But that would be off topic for SO. – TigerhawkT3 Sep 05 '21 at 11:49
  • @TigerhawkT3 I think it is reasonable to speculate that the reason is that if 'del' would be a method then it would be used like aList.del(aList.slice) ie. the list would have to be referenced twice which invites error and does not look cool ;) And since they had to have 'del' anyway to delete variables etc it was used for this too, So IMO this could have had an "educated guess" kind of answer. – nyholku Sep 06 '21 at 12:07