Why is del an instruction and not a method in python?

Why python creators preferred this syntax (instruction)

del list[index] 

over this (method)?

 list.del(index) 

It seems to me that del belongs to the same β€œcategory” as append , remove , find , etc and therefore should have the same syntax (be a method), but for some reason the creators of python implemented it as an instruction. Why would they do that?

+5
source share
1 answer

Because del is an instruction that you can delete several things with it, and since when you want to remove list_name[index] with del , you really want to delete the object, and this is the task that del does for other objects, therefore there is no need to create redundant attribute for lists for this!

An exception is recursively defined very similar to how an assignment is defined. Below are some tips.

Deleting a target list recursively deletes each target from left to right.

Deleting a name removes the binding of this name from the local or global namespace, depending on whether the name occurs in a global expression in the same code block. If the name is unbound, a NameError exception will be thrown.

Removing links to attributes, subscriptions, and sections is passed to the main object; deleting a section is generally equivalent to assigning an empty fragment of the correct type (but even this is determined by the truncated object).

+1
source

Source: https://habr.com/ru/post/1232224/


All Articles