keyword - What is the use case for "pass" in Python? -
this question has answer here:
- how use pass statement in python 12 answers
based on answers this question, pass
keyword in python absolutely nothing. indicates "nothing done here."
given this, don't understand use of it. example, i'm looking @ following block of code while trying debug script else wrote:
def dccount(server): ssh_cmd='ssh user@subserver.st%s' % (server) cmd='%s "%s"' % (ssh_cmd, sub_cmd) output=popen (cmd, shell=true, stdout=pipe) result=output.wait() queryresult="" if result == 0: queryresult = output.communicate()[0].strip() else: pass takedata(server, "dc", queryresult)
is there purpose @ having else: pass
here? in way change way function runs? seems if
/else
block rewritten following absolutely no change:
if result == 0: queryresult = output.communicate()[0].strip() takedata(server, "dc", queryresult)
... or missing something? and, if i'm not missing something, why ever want use pass
keyword @ all?
it indeed useless in example.
it helpful if want block empty, not otherwise allowed python. instance, when defining own exception subclass:
class myexception(exception): pass
or maybe want loop on iterator side effects, nothing results:
for _ in iterator: pass
but of time, won't need it.
remember if can add else isn't comment, may not need pass. empty function, instance, can take docstring , work block:
def nop(): """this function nothing, intentionally."""
Comments
Post a Comment