How to use the pass statement?

前端 未结 15 2368
旧时难觅i
旧时难觅i 2020-11-22 15:03

I am in the process of learning Python and I have reached the section about the pass statement. The guide I\'m using defines it as being a Null sta

相关标签:
15条回答
  • 2020-11-22 15:35

    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.")

    def some_silly_transform(n):
        # Even numbers should be divided by 2
        if n % 2 == 0:
            n /= 2
            flag = True
        # Negative odd numbers should return their absolute value
        elif n < 0:
            n = -n
            flag = True
        # Otherwise, number should remain unchanged
        else:
            pass
    

    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!) not to put redundant steps outside of the conditional statements. In the example above, flag is set in the two specifically mentioned cases, but not in the else-case. Without using pass, a future programmer might move flag = True to outside the condition—thus setting flag in all cases.


    Another case is with the boilerplate function often seen at the bottom of a file:

    if __name__ == "__main__":
        pass
    

    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:

    try:
        n[i] = 0
    except IndexError:
        pass
    
    0 讨论(0)
  • 2020-11-22 15:35

    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.g.

    class Error(Exception):
        pass
    

    So you can raise and catch Error exceptions. What matters here is the type of exception, rather than the content.

    0 讨论(0)
  • 2020-11-22 15:36

    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:

    def foo(x,y):
        return x+y
    

    means "if I call the function foo(x, y), sum the two numbers the labels x and y represent and hand back the result",

    def bar():
        pass
    

    means "If I call the function bar(), do absolutely nothing."

    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.

    c = a / b
    

    will, obviously, produce a ZeroDivisionError if b is 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:

    try:
        c = a / b
    except ZeroDivisionError:
        pass
    

    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... in statement. So:

    for t in range(25):
        do_a_thing(t)
        if t == 20:
            pass
    

    with the breakpoint on pass.

    0 讨论(0)
提交回复
热议问题