[Python-ideas] except expression
Steven D'Aprano
steve at pearwood.info
Mon Feb 17 06:07:27 CET 2014
On Sun, Feb 16, 2014 at 06:47:20PM -0800, Ethan Furman wrote:
> On 02/15/2014 10:11 AM, Steven D'Aprano wrote:
> >
> >Certainly not! pass implies that *no return result is generated at all*,
> >[...]
>
> Don't be silly.
>
> def have_a_mint(some, args, here):
> # flesh this out later
> pass
>
> Does anybody really think that that function will not return None?
But it isn't the *pass* that generates the "return None". It's falling
out the bottom of the function that generates the "return None".
Do you intend for this function to return None?
def trouble_down_mine(args):
pass
return "NOBODY expects the Spanish Inquisition!!!"
"pass" does not mean "return from this function", as I'm sure you know.
That's why I object to using "pass" in a form that implies "return
None". It is a placeholder, used to satisfy the lexer/parser, and gets
compiled out. As far as the compiler is concerned, "pass" simply
disappears when compiled:
py> from dis import dis
py> dis("pass; spam")
1 0 LOAD_NAME 0 (spam)
3 POP_TOP
4 LOAD_CONST 0 (None)
7 RETURN_VALUE
You may notice that the compiler currently adds a (redundant?) "return
None" to everything(?). That includes "pass", and nothing at all:
py> dis("pass")
1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
py> dis("")
1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
--
Steven
More information about the Python-ideas
mailing list