Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15847 > unrolled thread
| Started by | John Ladasky <ladasky@my-deja.com> |
|---|---|
| First post | 2011-11-17 18:18 -0800 |
| Last post | 2011-11-18 13:03 +0000 |
| Articles | 16 — 10 participants |
Back to article view | Back to comp.lang.python
What exactly is "pass"? What should it be? John Ladasky <ladasky@my-deja.com> - 2011-11-17 18:18 -0800
Re: What exactly is "pass"? What should it be? Chris Rebert <clp2@rebertia.com> - 2011-11-17 18:45 -0800
Re: What exactly is "pass"? What should it be? John Ladasky <ladasky@my-deja.com> - 2011-11-17 21:03 -0800
Re: What exactly is "pass"? What should it be? John Ladasky <ladasky@my-deja.com> - 2011-11-17 21:03 -0800
Re: What exactly is "pass"? What should it be? Chris Angelico <rosuav@gmail.com> - 2011-11-18 13:59 +1100
Re: What exactly is "pass"? What should it be? alex23 <wuwei23@gmail.com> - 2011-11-17 22:04 -0800
Re: What exactly is "pass"? What should it be? Dominic Binks <dbinks@codeaurora.org> - 2011-11-17 19:01 -0800
Re: What exactly is "pass"? What should it be? Ben Finney <ben+python@benfinney.id.au> - 2011-11-18 14:45 +1100
Re: What exactly is "pass"? What should it be? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-11-17 20:34 -0800
Re: What exactly is "pass"? What should it be? John Ladasky <ladasky@my-deja.com> - 2011-11-17 21:07 -0800
Re: What exactly is "pass"? What should it be? Chris Angelico <rosuav@gmail.com> - 2011-11-18 16:34 +1100
Re: What exactly is "pass"? What should it be? Chris Rebert <clp2@rebertia.com> - 2011-11-17 21:49 -0800
Re: What exactly is "pass"? What should it be? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-18 06:07 +0000
Re: What exactly is "pass"? What should it be? John Ladasky <ladasky@my-deja.com> - 2011-11-17 21:07 -0800
Re: What exactly is "pass"? What should it be? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-11-18 10:57 +0100
Re: What exactly is "pass"? What should it be? MRAB <python@mrabarnett.plus.com> - 2011-11-18 13:03 +0000
| From | John Ladasky <ladasky@my-deja.com> |
|---|---|
| Date | 2011-11-17 18:18 -0800 |
| Subject | What exactly is "pass"? What should it be? |
| Message-ID | <27955957.352.1321582691251.JavaMail.geo-discussion-forums@prap37> |
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.
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?
And would there be any merit to having some syntactic sugar which allows pass to behave like the _pass() function I wrote, if it were called?
As you can see, I'm programming in Python 2.6. I don't know whether pass is handled differently in Python 3.
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-11-17 18:45 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2813.1321584361.27778.python-list@python.org> |
| In reply to | #15847 |
On Thu, Nov 17, 2011 at 6:18 PM, John Ladasky <ladasky@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? Correct: http://docs.python.org/reference/simple_stmts.html#pass http://docs.python.org/reference/lexical_analysis.html#keywords Cheers, Chris
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <ladasky@my-deja.com> |
|---|---|
| Date | 2011-11-17 21:03 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <19287522.9.1321592603810.JavaMail.geo-discussion-forums@prfb5> |
| In reply to | #15850 |
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.
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <ladasky@my-deja.com> |
|---|---|
| Date | 2011-11-17 21:03 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2820.1321592614.27778.python-list@python.org> |
| In reply to | #15850 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-11-18 13:59 +1100 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2814.1321585155.27778.python-list@python.org> |
| In reply to | #15847 |
On Fri, Nov 18, 2011 at 1:18 PM, John Ladasky <ladasky@my-deja.com> 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
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2011-11-17 22:04 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <eebbe22c-a9a2-4a1f-b7cf-7a9f6475848a@y14g2000prf.googlegroups.com> |
| In reply to | #15851 |
On Nov 18, 12:59 pm, Chris Angelico <ros...@gmail.com> 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"""
[toc] | [prev] | [next] | [standalone]
| From | Dominic Binks <dbinks@codeaurora.org> |
|---|---|
| Date | 2011-11-17 19:01 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2815.1321585339.27778.python-list@python.org> |
| In reply to | #15847 |
On 11/17/2011 6:45 PM, Chris Rebert wrote:
> On Thu, Nov 17, 2011 at 6:18 PM, John Ladasky<ladasky@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@codeaurora.org
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-11-18 14:45 +1100 |
| Message-ID | <87pqgq6qlc.fsf@benfinney.id.au> |
| In reply to | #15847 |
John Ladasky <ladasky@my-deja.com> 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. <URL: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
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-11-17 20:34 -0800 |
| Message-ID | <mailman.2818.1321590908.27778.python-list@python.org> |
| In reply to | #15847 |
On Thu, 17 Nov 2011 18:18:11 -0800 (PST), John Ladasky
<ladasky@my-deja.com> 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
(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...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <ladasky@my-deja.com> |
|---|---|
| Date | 2011-11-17 21:07 -0800 |
| Message-ID | <3758934.65.1321592843731.JavaMail.geo-discussion-forums@prms22> |
| In reply to | #15861 |
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 > <lad...@my-deja.com> 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?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-11-18 16:34 +1100 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2823.1321594454.27778.python-list@python.org> |
| In reply to | #15866 |
On Fri, Nov 18, 2011 at 4:07 PM, John Ladasky <ladasky@my-deja.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-11-17 21:49 -0800 |
| Subject | Re: What exactly is "pass"? What should it be? |
| Message-ID | <mailman.2824.1321595391.27778.python-list@python.org> |
| In reply to | #15866 |
On Thu, Nov 17, 2011 at 9:34 PM, Chris Angelico <rosuav@gmail.com> wrote: > On Fri, Nov 18, 2011 at 4:07 PM, John Ladasky <ladasky@my-deja.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-11-18 06:07 +0000 |
| Message-ID | <4ec5f619$0$29967$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #15866 |
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
[toc] | [prev] | [next] | [standalone]
| From | John Ladasky <ladasky@my-deja.com> |
|---|---|
| Date | 2011-11-17 21:07 -0800 |
| Message-ID | <mailman.2821.1321592848.27778.python-list@python.org> |
| In reply to | #15861 |
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 > <lad...@my-deja.com> 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?
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2011-11-18 10:57 +0100 |
| Message-ID | <ja5a6s$7at$1@r03.glglgl.gl> |
| In reply to | #15861 |
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_FAST 0 (a)
3 POP_JUMP_IF_FALSE 16
6 LOAD_FAST 0 (a)
9 CALL_FUNCTION 0
12 POP_TOP
13 JUMP_FORWARD 0 (to 16)
3 >> 16 ...
as opposed to just
2 0 LOAD_FAST 0 (a)
3 CALL_FUNCTION 0
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 :-(
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-11-18 13:03 +0000 |
| Message-ID | <mailman.2826.1321621390.27778.python-list@python.org> |
| In reply to | #15847 |
On 18/11/2011 04:34, Dennis Lee Bieber wrote: > On Thu, 17 Nov 2011 18:18:11 -0800 (PST), John Ladasky > <ladasky@my-deja.com> 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... >
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web