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

2011-11-18 Thread MRAB

On 18/11/2011 04:34, Dennis Lee Bieber wrote:

On Thu, 17 Nov 2011 18:18:11 -0800 (PST), John Ladasky
  declaimed the following in
gmane.comp.python.general:


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


This is the equivalent of

def _pass(*args):
return None


Wouldn't "pass_" be a more Pythonic name?


(functions with no explicit return statement implicitly return None)


def long_running_process(arg1, arg2, arg_etc, report = _pass):
 result1 = do_stuff()
 report(result1)


So this is a call to a function that just returns a None, which is
dropped by the interpreter...



--
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-18 Thread Thomas Rachel

Am 18.11.2011 05:34 schrieb Dennis Lee Bieber:
>> def _pass(*args):
>>  pass
>>
>> def long_running_process(arg1, arg2, arg_etc, report = _pass):
>>  result1 = do_stuff()
>>  report(result1)
>
> So this is a call to a function that just returns a None, which is
> dropped by the interpreter...

I'm not sure about this. I think, the call will be executed, but the 
callee will return immediately. It is different from call being dropped.


Another optimized alternative could be to do

def long_running_process(arg1, arg2, arg_etc, report=None):
result1 = do_stuff()
if report: report(result1)

but I think that is too low benefit at the cost of too much readability.

Such a function call is

  2   0 LOAD_FAST0 (a)
  3 POP_JUMP_IF_FALSE   16
  6 LOAD_FAST0 (a)
  9 CALL_FUNCTION0
 12 POP_TOP
 13 JUMP_FORWARD 0 (to 16)

  3 >>   16 ...

as opposed to just

  2   0 LOAD_FAST0 (a)
  3 CALL_FUNCTION0
  6 POP_TOP

with a call target of

  1   0 LOAD_CONST   0 (None)
  3 RETURN_VALUE

I don't think that a call is sooo expensive that it would make any 
noticeable difference.



Thomas

BTW: Sorry, Dennis, for the private mail - I am incapable to use 
Thunderbird correctly :-(

--
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Steven D'Aprano
On Thu, 17 Nov 2011 21:07:23 -0800, John Ladasky wrote:

> One of my questions was: would there be any merit to having the Python
> "pass" token itself defined exactly as _pass() is defined above?

No.

The pass statement compiles to nothing at all. Your _pass() function 
compiles to a function object, which needs to be looked up at run time, 
then called, all of which takes time and memory.

To satisfy the compiler, but do nothing, the pass statement should stay a 
statement. When you need a "do nothing" function, either define one (two 
lines) in your application, or use a lambda in place (lambda *args: 
None). Either way, it is too trivial to be a built-in.

By the way, to answer your earlier question "what is pass?", you could do 
this in the interactive interpreter:

help("pass")




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread alex23
On Nov 18, 12:59 pm, Chris Angelico  wrote:
> If you call your dummy function something else, it may help
> readability/self-documentation too.

Or replace the pass with a docstring for the same effect:

  def silent(*args):
"""Null Object to repress reporting"""

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Chris Rebert
On Thu, Nov 17, 2011 at 9:34 PM, Chris Angelico  wrote:
> On Fri, Nov 18, 2011 at 4:07 PM, John Ladasky  wrote:
>> One of my questions was: would there be any merit to having the Python 
>> "pass" token itself defined exactly as _pass() is defined above?
>
> No, there wouldn't. The Python 'pass' statement is a special statement
> that indicates a lack of anything to execute; a dummy function call
> isn't this. What I would kinda like to see, though, is function
> versions of many things. Your basic operators exist in the 'operator'
> module, but the syntax is rather clunky; for comparison, Pike has
> beautifully simple (if a little cryptic) syntax: back-tick followed by
> the operator itself, very similar to the way C++ does operator
> overloading.
>
> In Python 2, back-tick has a special meaning. In Python 3, that
> meaning is removed. Is the character now available for this
> "function-version" syntax?

Negative. I know this from personal experience.

Things that will Not Change in Python 3000
(http://www.python.org/dev/peps/pep-3099/ ):
"No more backticks."

Cheers,
Chris R.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Chris Angelico
On Fri, Nov 18, 2011 at 4:07 PM, John Ladasky  wrote:
> One of my questions was: would there be any merit to having the Python "pass" 
> token itself defined exactly as _pass() is defined above?

No, there wouldn't. The Python 'pass' statement is a special statement
that indicates a lack of anything to execute; a dummy function call
isn't this. What I would kinda like to see, though, is function
versions of many things. Your basic operators exist in the 'operator'
module, but the syntax is rather clunky; for comparison, Pike has
beautifully simple (if a little cryptic) syntax: back-tick followed by
the operator itself, very similar to the way C++ does operator
overloading.

In Python 2, back-tick has a special meaning. In Python 3, that
meaning is removed. Is the character now available for this
"function-version" syntax?

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread John Ladasky
On Thursday, November 17, 2011 8:34:22 PM UTC-8, Dennis Lee Bieber wrote:
> On Thu, 17 Nov 2011 18:18:11 -0800 (PST), John Ladasky
>  declaimed the following in
> gmane.comp.python.general:
> > def _pass(*args):
> > pass
> >
>   This is the equivalent of
> 
> def _pass(*args):
>   return None
> 
> (functions with no explicit return statement implicitly return None)

OK, that works for me, and now I understand.

One of my questions was: would there be any merit to having the Python "pass" 
token itself defined exactly as _pass() is defined above?

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread John Ladasky
On Thursday, November 17, 2011 6:45:58 PM UTC-8, Chris Rebert wrote:

> 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.

Oops, I over-simplified the calls to my report() function.  The truth is that 
report() can accept a variable argument list too.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Ben Finney
John Ladasky  writes:

> So, pass does not appear to be a function, nor even an object. Is it
> nothing more than a key word?

Yes. Unlike some languages where the program is a collection of
expressions, a Python program is a series of statements which themselves
may or may not be expressions.

http://docs.python.org/reference/lexical_analysis.html>

-- 
 \  “I tell you the truth: some standing here will not taste death |
  `\before they see the Son of Man coming in his kingdom.” —Jesus, |
_o__) c. 30 CE, as quoted in Matthew 16:28 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Dominic Binks

On 11/17/2011 6:45 PM, Chris Rebert wrote:

On Thu, Nov 17, 2011 at 6:18 PM, John Ladasky  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 :

  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:
 
  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: dbi...@codeaurora.org
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Chris Angelico
On Fri, Nov 18, 2011 at 1:18 PM, John Ladasky  wrote:
> def _pass(*args):
>    pass
>
> def long_running_process(arg1, arg2, arg_etc, report = _pass):
>

For some compactness at the expense of brevity, you could use a lambda:

def long_running_process(arg1, arg2, arg_etc, report = lambda msg: None):

Other than that, I think it's fine. (Actually, the lambda has a slight
advantage in self-documentation; in the main function's definition it
specifies that the 'report' argument is a function that takes one
argument, the message. (Or whatever that argument is. Name it
appropriately.)

If you call your dummy function something else, it may help
readability/self-documentation too. On the other hand, it may not.
YMMV.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-11-17 Thread Chris Rebert
On Thu, Nov 17, 2011 at 6:18 PM, John Ladasky  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?

Correct:
http://docs.python.org/reference/simple_stmts.html#pass
http://docs.python.org/reference/lexical_analysis.html#keywords

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list