Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a standard way to do a no-op in python?

Tags:

python

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?

like image 283
Ben Avatar asked Mar 27 '09 17:03

Ben


People also ask

Is there a no op in Python?

In Python programming, the pass statement is a null statement. The difference between a comment and a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored. However, nothing happens when the pass is executed. It results in no operation (NOP).

How do I know if I have nothing in Python?

In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement. We can use pass in empty while statement also.


1 Answers

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 
like image 66
Brian R. Bondy Avatar answered Sep 25 '22 09:09

Brian R. Bondy