1

I've seen 'del' being referred to as a keyword and as a statement. Apparently, it is not considered a function:

Python has a del statement (keyword) to delete objects, not a del function.

https://tutorial.eyehunts.com/python/del-function-in-python-code/



I looked up the definition of a function in Python and it says:

In Python, a function is a group of related statements that performs a specific task.


Under this definition, I do not understand how del cannot be considered a function.
It does perform a specific task: deleting an item at a given index.

The syntax appears to be different, as you don't need parentheses to use the del statement:

motorcycles = ['honda', 'suzuki', 'yamaha']
del motorcycles[0] 

But what are the other practical consequences of del being a statement/keyword rather than a function?



Finally, the main question here is: how do I identify something as a statement rather than a function and vice versa?

Considering that I'll have to know that in order to know the syntax needed, I think it is quite important for me to understand that.



I did some research and I found a few explanations to why functions are different than keywords:

Function is a lower designation than a keyword interaction - and in of itself - has to call higher designated System operative calls or akin - to fundamentally interact with memory partitioning.

The main reason for this - is how Python’s hierarchy is constructed in of itself.

But to me - a beginner in Python whose first programming language is R - that does not seem to help me identify something as a keyword instead of a function.

Plus, if a statement is different than a keyword, how is it that I've seen del being referred to as both a statement AND a keyword?

(What is the difference between a statement and a keyword?)

Can anyone shed some light on this, please? I'd really appreciate it!

  • 1
    I think that `del` is a statement because it is not called, whereas functions have to be called to get the `group of related statements` to run. – quamrana Apr 17 '22 at 14:19
  • 1
    @quamrana is right since statement performs an action (in this case deleting objects). Additionally, `del` is not a function because a function (as you said it) comprise of group of related statements that performs a specific task. `del` is a single statement. – VRComp Apr 17 '22 at 14:39

1 Answers1

2

Python keywords are essentially special reserved words that have specific meanings and purposes and can’t be used for anything but those specific purposes. They come with the python standard library and you cannot change them in anyway. You can read more about keywords here If you run help("keywords") in your python interpreter, a bunch of keywords will be returned, and del is one of them.

Python statements refer to any line/lines of python code (which include stuff such as del motorcycles[0] and print(motorcycles[0])), and they do not have to meet any specific conditions to be classified as statements.

Python functions refer to a block of reusable code that does something and can returns a value each time they are run. They can either come with the standard library or be defined by users. You can refer to this for more information about functions.

Del in python is a keyword as it does something a normal python function cannot do: it interacts with the underlying memory and unbinds the variable from the memory. It is implemented with CPython.

SmartOinker
  • 136
  • 7
  • what do you mean when you say del interacts with the underlying memory? Could you give me an example? – Marina Bonatti Apr 17 '22 at 14:56
  • 1
    This question would require a deeper understanding of how python works. This answer gives us a good understanding of how del is implemented in python: https://stackoverflow.com/a/21053433/14850526 – SmartOinker Apr 17 '22 at 15:38