How to use the pass statement
Suppose you are designing a new class with some methods that you don't want to implement, yet.
class MyClass(object):
def meth_a(self):
pass
def meth_b(self):
print "I'm meth_b"
If you were to leave out the pass
, the code wouldn't run.
You would then get an:
IndentationError: expected an indented block
To summarize, the pass
statement does nothing particular, but it can act as a placeholder, as demonstrated here.
How to use the pass statement
Suppose you are designing a new class with some methods that you don't want to implement, yet.
class MyClass(object):
def meth_a(self):
pass
def meth_b(self):
print "I'm meth_b"
If you were to leave out the pass
, the code wouldn't run.
You would then get an:
IndentationError: expected an indented block
To summarize, the pass
statement does nothing particular, but it can act as a placeholder, as demonstrated here.
Define Pass statement in Python?
pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
The pass statement in python acts as a placeholder for your future code. As the body of the loops and if condition cannot be empty in python, they cannot be left empty. However, it is possible that you have not yet decided what will happen if an if condition
gets true. Then, you can use pass
to avoid getting error and your code will not break.
# An if condition where the action is yet not known
if my_test_var == desired value:
pass
What is the use case for pass in Python?
It is indeed useless in your example.
It is sometimes helpful if you want a block to be empty, something not otherwise allowed by Python. For instance, when defining your own exception subclass:
class MyException(Exception):
pass
Or maybe you want to loop over some iterator for its side effects, but do nothing with the results:
for _ in iterator:
pass
But most of the time, you won't need it.
Remember that if you can add something else that isn't a comment, you may not need pass. An empty function, for instance, can take a docstring and that will work as a block:
def nop():
"""This function does nothing, intentionally."""
Why I can't use pass in python if else statement when use condition in a single line
There is a difference between an if
statement and a conditional expression.
When you write
if condition:
this()
else:
that()
you are writing a statement. It is an imperative construct — it does something. And the else
bit is optional; if it is omitted, the statement will do nothing if the condition is not fulfilled.
The this if condition else that
construct is an expression. It computes a value. It always has to compute a value, so the else
bit has to be there. Using it for its side effects, for example by calling `list.append(), which doesn't return anything useful, is... not exactly wrong, but... well, it is wrong. Not technically, but philosophically.
So, when you want to compute a value, in one of two possible ways, and the whole thing fits in a line or so, use a conditional expression. If you want to do either of two things, one of which may be nothing, use an if statement.
Notice that you write
if temperature < 0:
coat = thick
else:
coat = thin
but
coat = thick if temperature < 0 else thin
In the first case, you have an if statement that selects which of two assignment statements to execute. In the second case, you have a single assignment statement, which uses a conditional expression to decide which value to assign.
Expressions can be used as statements (in which case their value is simply ignored), but statements have no value, and thus cannot be used as expressions.
What the purpose of the `pass` statement?
None
already has meaning in Python. It is not a statement, but a value. It can be used to flag a variable that does not yet have a standard value, or as the "return value" of a function that does not return anything.
So using it as a statement would be more confusing than using the new keyword pass
.
Pass statement in one-line Python if-else statement
pass
is a statement, not a value, so it cannot be used in a conditional expression.
You want to use the filter clause of the list comprehension.
[col for col in cols if 'name' in col]
It is somewhat unfortunate that the keyword if
is used in three distinct syntactic constructs (if
statements, conditional expressions, and the iteration construct shared by list/dict/set comprehensions and generator expressions).
Related Topics
Hexadecimal String to Byte Array in Python
Checking Multiple Values for a Variable
How to Get User Ip Address in Django
String Replace Doesn't Appear to Be Working
Insert Line at Middle of File with Python
Understanding Nested List Comprehension
Python 2.7 Getting User Input and Manipulating as String Without Quotations
Using a Dictionary to Count the Items in a List
Create Nice Column Output in Python
Efficiently Using Multiple Numpy Slices for Random Image Cropping
How to Lowercase a String in Python
Python List VS. Array - When to Use
Sort() and Reverse() Functions Do Not Work
Efficient String Matching in Apache Spark
Python Nameerror: Name Is Not Defined
How to Load Default Profile in Chrome Using Python Selenium Webdriver