Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dummy statement for nothing or nop when indent expected [duplicate]

Tags:

python

I recall there was a dummy statement that was the equivalent of do nothing or fill in the blank space after the if, elif, else, and for statements to keep the expected indentation.

The below example will not work

if True:
    #I want to simply pass this branch
    # ... NOP command here
else:
    print "False"

How can I achieve this?

like image 707
Radu Ionescu Avatar asked Mar 09 '16 13:03

Radu Ionescu


3 Answers

There's pass:

def foo():
    pass

In Python 3 there's also the ellipsis ...*, which wasn't really meant for this, but is also sometimes used:

def foo():
    ...

Semantically, I would expect to see ... when that part isn't written yet, as a stub, and pass when there's never going to be code there.


* The ellipsis also exists in Python 2, but can't be used outside the square brackets in something like foobar[...].

like image 108
L3viathan Avatar answered Nov 12 '22 04:11

L3viathan


You can use the pass command to achieve this

if True:
    pass
else:
    print "False"
like image 4
PKuhn Avatar answered Nov 12 '22 03:11

PKuhn


You can use the pass statement like this:

if True:
    pass
else:
    print "False"
like image 2
Mark Skelton Avatar answered Nov 12 '22 03:11

Mark Skelton