What exactly is "pass"? What should it be?

Dominic Binks dbinks at codeaurora.org
Thu Nov 17 22:01:01 EST 2011


On 11/17/2011 6:45 PM, Chris Rebert wrote:
> On Thu, Nov 17, 2011 at 6:18 PM, John Ladasky<ladasky at my-deja.com>  wrote:
>> Hi folks,
>>
>> I'm trying to write tidy, modular code which includes a long-running process.  From time to time I MIGHT like to check in on the progress being made by that long-running process, in various ways.  Other times, I'll just want to let it run.  So I have a section of code which, generally, looks like this:
>>
>> def _pass(*args):
>>     pass
>>
>> def long_running_process(arg1, arg2, arg_etc, report = _pass):
>>     result1 = do_stuff()
>>     report(result1)
>>     result2 = do_some_different_stuff()
>>     report(result2)
>>     result3 = do_even_more_stuff()
>>     report(result3)
>>     return result3
>>
>> This does what I want.  When I do not specify a report function, the process simply runs.  Typically, when I do supply a report function, it would print something to stdout, or draw an update through a GUI.
>>
>> But this approach seems a tad cumbersome and unPythonic to me, particularly the part where I define the function _pass() which accepts an arbitrary argument list, and does nothing but... pass.
>
> Seems fine to me (good use of the null object pattern), although I
> might define _pass() to instead take exactly 1 argument, since that's
> all you ever call report() with in your example.
>
>> This has led me to ask the question, what exactly IS pass?  I played with the interpreter a bit.
>>
>> IDLE 2.6.6      ==== No Subprocess ====
>>>>> pass
>>>>> pass()
>> SyntaxError: invalid syntax
>>>>> type(pass)
>> SyntaxError: invalid syntax
>>
>> So, pass does not appear to be a function, nor even an object.  Is it nothing more than a key word?
>
It is a keyword that can appear in a position where a statement is 
required by the grammar but there is nothing to do.  For example if .. 
then .. else .. where nothing happens in the else condition is effectively:

   if <condition>:
  	<then-part>
   else:
	pass

Bourne shell has a similar construct with the colon statement :

Another python example is where you need to catch an exception (or all 
exceptions but don't actually care about what they are)

   try:
      <european swallows>
   except:
      pass

> Correct:
> http://docs.python.org/reference/simple_stmts.html#pass
> http://docs.python.org/reference/lexical_analysis.html#keywords
>
> Cheers,
> Chris


-- 
Dominic Binks: dbinks at codeaurora.org
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum



More information about the Python-list mailing list