1

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.

TusharK
  • 25
  • 4
  • 2
    Before asking for the difference, could you explain your understanding of what a function is? – Mad Physicist Feb 13 '20 at 16:11
  • 4
    "Instruction" isn't a defined term in the Python language documentation; `del ...` is a statement. – kaya3 Feb 13 '20 at 16:11
  • Related: [Expression Versus Statement](https://stackoverflow.com/questions/19132/expression-versus-statement) – kaya3 Feb 13 '20 at 16:15
  • @MadPhysicist A function is a piece of code that is called by name. It is not associated with any object like a method. It can be passed data to operate on. Please correct me if I am wrong. – TusharK Feb 13 '20 at 16:22
  • I am learning from https://pythoninstitute.org/free-python-courses/ del is written as instruction. – TusharK Feb 13 '20 at 16:25
  • 1
    @tusharkhoche Then I would be wary of using that as a learning resource. There is a [`del` statement](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement), so named because it is recognized by the use of the `del` keyword. – chepner Feb 13 '20 at 16:43

1 Answers1

1

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.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53