TusharK
TusharK

Reputation: 25

What's the difference between an instruction and a function?

I have just started learning Python. I came across del instruction. I could not understand difference between an instruction and a function like len(). I googled it but could not find the answer. Apologies if this question sounds childish.

Upvotes: 1

Views: 1006

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

Things like del, def, class, lambda, with, for, while, etc. are all similar in that they are essentially special cases. They all have specifically-defined behavior, but that behavior is hardcoded into the python interpreter.

In the case of def, that behavior is to parse the following text in a certain way - as a function. Similar for lambda, as a lambda function, and for class, as a class.

In the case of with, the behavior is to take an argument, call .__enter__() on it, and assign the result to a name that's given after the as keyword. With for, it's the opposite - define the name before the in keyword, and then call .__iter__() on whatever's after, assigning each item in turn to the name.

del is similar, in that it has a built-in function - it removes the object from the current namespace. That's just what it does, hardcoded behavior of the interpreter. There are a couple of special cases baked in for iterable objects, in which case (IIRC) the compiler calls .__delitem__() on it.


Functions are a specific type of object with a specific programmer-defined behavior. They're defined with the def instruction.

Upvotes: 1

Related Questions