Python Pass

Python Pass Statement

The pass keyword in Python is a null statement which means that it does not do anything in the code.

When it gets executed, Python moves on to the next line. This statement is used to avoid errors when you are writing an empty loop, a class, a method, etc.


Python Pass

Our value of x is equal to 3, and when the condition of the if statement turned out to be True, then the pass statement got executed. Whenever the value of x is not equal to 3, the code will move on to the else condition of the code.

Ironically, our code does not print anything but without the pass statement Python would raise an error.

The pass statement can also work as a placeholder for the code that we want to write in the future.


Python Pass placeholder

Let us add some functionality to our code now.


Python Pass print

You can do the same while creating a class.


Python Pass by creating class

The code does not do anything but also does not give us an error.

Pass by Value or Pass by Reference

Learners who are coming from other programming languages like C++ might ask a question whether the model of passing arguments in Python is Pass by Value or Pass by Reference.

The thing is, neither of them is true, actually, Python uses the model Pass by Object Reference.

 Let us see what each of them means.

Pass by Value

When we say passing by value, we mean that a copy of the actual parameter in the memory of the system is made and passed wherever it has to be used.

Pass by Reference

Passing by Reference means that the parameter passed is the copy of the address of the actual parameter in the memory.

Now finally comes the Pass by Object Reference model that is used by Python.

Pass by Object Reference

The property that Python is an object-oriented language, when you pass an argument in the function, the function in Python gets the reference of the object of the argument in the memory.

Tutorials for all brains!