210

I often find myself writing if / elif / else constructs in python, and I want to include options which can occur, but for which the corresponding action is to do nothing. I realise I could just exclude those if statements, but for readability I find it helps to include them all, so that if you are looking through the code you can see what happens as a result of each option. How do I code the no-op? Currently, I'm doing it like this:

no_op = 0

if x == 0:
    y = 2 * a
elif x == 1:
    z = 3 * b
elif x == 3:
    no_op

(The code is actually quite a bit longer than that, and more complicated. This is just to illustrate the structure).

I don't like using a variable as a no-op, but it's the neatest way I could think of. Is there a better way?

Ben
  • 66,838
  • 37
  • 84
  • 108

7 Answers7

380

Use pass for no-op:

if x == 0:
  pass
else:
  print "x not equal 0"

And here's another example:

def f():
  pass

Or:

class c:
  pass
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
29

How about pass?

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
27

If you need a function that behaves as a nop, try

nop = lambda *a, **k: None
nop()

Sometimes I do stuff like this when I'm making dependencies optional:

try:
    import foo
    bar=foo.bar
    baz=foo.baz
except:
    bar=nop
    baz=nop

# Doesn't break when foo is missing:
bar()
baz()
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
  • 2
    This is a good workaround when one needs the `nop` in a context which specifically requires an expression (versus an arbitrary statement; q.v. https://docs.python.org/3/reference/simple_stmts.html and https://docs.python.org/3/reference/compound_stmts.html) – like e.g. as a sub-expression to an existing code idiom like a comprehension, or a `lambda`, or (god forbid) a string to be passed into `eval(…)` (which q.v. https://docs.python.org/3/library/functions.html#eval if you must). – fish2000 Apr 23 '18 at 23:26
  • 2
    Using `lambda` to assign a function to a variable for reusage is bad coding style. There's `def` for that. – Bachsau Jul 17 '20 at 11:22
2

You can print an empty string and no line feed to the standard output. This can be handy if a linter prohibits you from using a pass where it is unnecessary, but you need a line for a comment:

print('', end='')  # This does next to nothing

Using this empty print has very little side effects, besides wasted CPU cycles:

$ echo "print('', end='')  # This does next to nothing" > main.py
$ python3 main.py
$ 

Note that using pass here would have been syntactically correct, but linters might complain:

$ echo "pass  # This does next to nothing" > main.py
$ python3 main.py
$ 
Bengt
  • 14,011
  • 7
  • 48
  • 66
1

... (builtins.ellipsis) was not mentioned as a viable alternative to pass in an expression context.

def f(condition: bool) -> None:
    if condition:
        print("hello ", end="")
    else:
        pass  # noop

    print("world") if condition else ...


f(False)  # ''
f(True)   # 'hello world'
Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
0

As 'return' without args is apparently a no-op, under some code, it junked me out in analysis of some obfuscated 'virus' in Python for some time.

-3

a simple practice on my desk just uses a line like this:

dummy = 0

the normal coder (even those which are coming from other common programming languages) will instantly understand it.

Alexander Stohr
  • 159
  • 1
  • 18
  • It is common amongst Earth sciences professionals to call invalid data values "dummy", so a `dummy = 0` in the code of such application will likely be interpreted by developers as zero being its no-data value. Hence, that's not exactly an universal convention. – Paulo Carvalho Aug 11 '23 at 22:34