473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

lambdas vs functions: a bytecode question

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

Jul 18 '05 #1
2 1162
Fernando Perez wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Sentimentality is a superstructure covering brutality.
-- C.G. Jung
Jul 18 '05 #2
Erik Max Francis wrote:
Fernando Perez wrote:
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.


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

Regards,

f

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gong | last post by:
hi i would like to pickle a lambda; according to the library docs in 2.3, i believe this shouldnt be possible, since a lambda is not a function defined at the top level of a module (?) ...
1
by: Daniel Brodie | last post by:
Hello, 1) I am trying to understand how yield works in a bytecode way. How exactly does the YIELD_VALUE instruction work? Does the function 'change' to return a generator at compile time...
6
by: John Fouhy | last post by:
So I'm trying to generate Tkinter callback functions on the fly, but it's not working, and I don't understand what's going on. Here is an example program: -------------------------- from...
5
by: LutherRevisited | last post by:
This may be a dumb question, but are there any practical advantages of compiling a python application to *.pyo, or *.pyc? I haven't noticed any difference in the performance of text *.py or a...
33
by: Maurice LING | last post by:
Hi, I've been using Python for about 2 years now, for my honours project and now my postgrad project. I must say that I am loving it more and more now. From my knowledge, Python bytecodes are...
15
by: Ingmar | last post by:
Simple comparison tests we have performed show that System.Math functions in C# are much slower than corresponding functions in C++. Extreme examples are System.Math.Exp() and System.Math.Tan(),...
4
by: Fabiano Sidler | last post by:
Hi folks! I'm looking for a way to compile python source to bytecode instead of code-objects. Is there a possibility to do that? The reason is: I want to store pure bytecode with no additional...
6
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count'...
5
by: Chris Johnson | last post by:
What I want to do is build an array of lambda functions, like so: a = (This is just a demonstrative dummy array. I don't need better ways to achieve the above functionality.) print ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.