lambdas vs functions: a bytecode question

F

Fernando Perez

Hi all,

there are a couple of threads on lambdas today, which got me curious about
their differences as far as bytecode goes:

planck[~]|2> lf=lambda x: x**2
planck[~]|3> def ff(x): return x**2
|.>
planck[~]|4> import dis
planck[~]|5> dis.dis(lf)
1 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (2)
6 BINARY_POWER
7 RETURN_VALUE
planck[~]|6> dis.dis(ff)
1 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (2)
6 BINARY_POWER
7 RETURN_VALUE
8 LOAD_CONST 0 (None)
11 RETURN_VALUE

Can someone explain to me what the extra two bytecodes at the end of the
function version (ff) are for?

This is just curiosity, please note that I am NOT making any arguments pro or
against lambdas, functions or anything else.

Cheers,

f
 
E

Erik Max Francis

Fernando said:
Can someone explain to me what the extra two bytecodes at the end of the
function version (ff) are for?

This is just curiosity, please note that I am NOT making any arguments pro or
against lambdas, functions or anything else.

It's returning None. I would guess that it's a safety net to make sure
you don't fall of the end of the function without returning anything;
that is, the code analysis is not in place to see whether it's necessary
to actually include in the bytecodes or not.
 
F

Fernando Perez

Erik said:
It's returning None. I would guess that it's a safety net to make sure
you don't fall of the end of the function without returning anything;
that is, the code analysis is not in place to see whether it's necessary
to actually include in the bytecodes or not.

Thanks, that makes sense, considering that the bytecode compiler in python does
very little in the way of flow analysis.

Regards,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top