Search code examples
pythonkeywordsemantics

What is the difference between Pass and None in Python


I would personally like to know the semantic difference between using Pass and None. I could not able to find any difference in execution.

PS: I could not able to find any similar questions in SO. If you find one, please point it out.

Thanks!


Solution

  • pass is a statement. As such it can be used everywhere a statement can be used to do nothing.

    None is an atom and as such an expression in its simplest form. It is also a keyword and a constant value for “nothing” (the only instance of the NoneType). Since it is an expression, it is valid in every place an expression is expected.

    Usually, pass is used to signify an empty function body as in the following example:

    def foo():
        pass
    

    This function does nothing since its only statement is the no-operation statement pass.

    Since an expression is also a valid function body, you could also write this using None:

    def foo():
        None
    

    While the function will behave identically, it is a bit different since the expression (while constant) will still be evaluated (although immediately discarded).