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!
CodePudding user response:
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.