How can you create a copy of an object in Python? How to avoid having class data shared among instances in Python? What is used to create Unicode string in Python? Difference between lists and tuples in Python? What are differences between List and Dictionary in Python Different file processing modes supported by Python How do you append to a file in Python?
What are the differences between the threading and multiprocessing? Is there any way to kill a Thread in Python? What is the use of lambda in Python? What is map, filter and reduce in python? Is monkey patching considered good programming practice? What is "typeerror: 'module' object is not callable" Python: TypeError: unhashable type: 'list' How to convert bytes to string in Python? What are metaclasses in Python? Besides its use as a placeholder for unimplemented functions, pass can be useful in filling out an if-else statement "Explicit is better than implicit.
Of course, in this case, one would probably use return instead of assignment, but in cases where mutation is desired, this works best. The use of pass here is especially useful to warn future maintainers including yourself! In the example above, flag is set in the two specifically mentioned cases, but not in the else -case. In some files, it might be nice to leave that there with pass to allow for easier editing later, and to make explicit that nothing is expected to happen when the file is run on its own.
Finally, as mentioned in other answers, it can be useful to do nothing when an exception is caught:. The best and most accurate way to think of pass is as a way to explicitly tell the interpreter to do nothing.
In the same way the following code:. The other answers are quite correct, but it's also useful for a few things that don't involve place-holding. For example, in a bit of code I worked on just recently, it was necessary to divide two variables, and it was possible for the divisor to be zero. In this particular situation, leaving c as zero was the desired behavior in the case that b was zero, so I used the following code:.
Another, less standard usage is as a handy place to put a breakpoint for your debugger. For example, I wanted a bit of code to break into the debugger on the 20th iteration of a for A common use case where it can be used 'as is' is to override a class just to create a type which is otherwise the same as the superclass , e. So you can raise and catch Error exceptions.
What matters here is the type of exception, rather than the content. So you can take advantage of it in a lot of places by making it a place holder:. Since the Python interpreter does not allow for a blank or unimplemented class, function, or conditional statement it gives an error:. You can say that pass means a NOP no operation operation. You will get a clear picture after this example:.
But your code will give an error because it required an indented block after elif. Here is the role of the pass keyword. The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
Honestly, I think the official Python docs describe it quite well and provide some examples:. For example:. Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:. If you want to import a module, if it exists, and ignore importing it, if that module does not exists, you can use the below code:. If you avoid writing the pass statement and continue writing the rest of your code, a IndentationError would be raised, since the lines after opening the except block are not indented.
In addition to the "main" use of "I don't want anything to go here," here's one I just found, assuming gen is an Iterator :. String Methods. Start Learning Python. Explore Python Examples. Python Assert Statement. Python Statement, Indentation and Comments. Python Multiple Inheritance. Python pass statement In this article, you'll learn about pass statement.
Video: Python pass Statement. What is pass statement in Python? However, nothing happens when the pass is executed. It results in no operation NOP. But the difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignored. The pass statement is generally used as a placeholder i. So user simply places pass at that line. So user can simply place pass where empty code is not allowed, like in loops, function definitions, class definitions, or in if statements.
So using pass statement user avoids this error. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. Skip to content. Change Language. Related Articles. Data Types.
Control Flow.
0コメント