473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: Getting a set of lambda functions

On Sun, 25 May 2008 18:46:27 -0700
John Nagle <na***@animats.comwrote:
>>func_strings=['x', 'x+1', 'x+2', 'x']
funclist = [eval('lambda x:' + func) for func in func_strings]

What are you actually trying to do, anyway? What's the
application?

You probably don't want to use "eval" that way. If you want
function objects out, use "compile".
I am writing on an application that I call pyspread
(http://pyspread.sourceforge.net).
It provides a grid (mapped to a numpy.array) into which users can type
in strings that contain python expressions.
Each expression is transformed into a function object in a second
numpy.array of the same size, so that each function can be called by
accessing the respective cell of the grid.

eval seems to work quite fine and provides nice access to globals,
system functions, modules, etc. Therefore, one can do general
programming tasks within the grid without worrying about cell
precedence.

compile returns code objects instead of function objects. I can of
course evaluate these code objects. However, when I store the code
objects, it is an extra operation each time I want to call the
function. So what is the benefit?

More specific: Is there any benefit of calling:

eval(compile(mystring, '', 'eval'))

instead of

eval(mystring)

?

What are the drawbacks of my approach that I may be missing?
Any suggestions?

Best Regards

Martin
Jun 27 '08 #1
1 1637
On May 27, 1:11*am, Martin Manns <mma...@gmx.netwrote:
On Sun, 25 May 2008 18:46:27 -0700

John Nagle <na...@animats.comwrote:
>>>func_strings=['x', 'x+1', 'x+2', 'x']
>>>funclist = [eval('lambda x:' + func) for func in func_strings]
* *What are you actually trying to do, anyway? *What's the
application?
* *You probably don't want to use "eval" that way. *If you want
function objects out, use "compile".

I am writing on an application that I call pyspread
(http://pyspread.sourceforge.net).
It provides a grid (mapped to a numpy.array) into which users can type
in strings that contain python expressions.
Each expression is transformed into a function object in a second
numpy.array of the same size, so that each function can be called by
accessing the respective cell of the grid.

eval seems to work quite fine and provides nice access to globals,
system functions, modules, etc. Therefore, one can do general
programming tasks within the grid without worrying about cell
precedence.

compile returns code objects instead of function objects. I can of
course evaluate these code objects. However, when I store the code
objects, it is an extra operation each time I want to call the
function. So what is the benefit?

More specific: Is there any benefit of calling:

eval(compile(mystring, '', 'eval'))

instead of

eval(mystring)

?

What are the drawbacks of my approach that I may be missing?
Any suggestions?

Best Regards

Martin
Yes, there is.
Your original example can be rewritten
>>func_strings=['x', 'x+1', 'x+2', 'x']
funclist = [compile('lambda x: %s' % func, '<string>', 'eval') for func in func_strings]
len(funclist)
4
>>len(set(funclist))
3
>>eval(funclist[0])(1)
1

Ivan
Jun 27 '08 #2

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

Similar topics

53
3601
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
4
1503
by: Charlie Taylor | last post by:
I find that I use lambda functions mainly for callbacks to things like integration or root finding routines as follows. flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd) ...
26
3439
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
4
7715
by: Paul MG | last post by:
Hi Is there a simple general way of getting the keys out of a map? Or a vector of pairs? My problem seems to be that the pair<> type returned by iterating through either has no methods to...
5
2505
by: Max Rybinsky | last post by:
Hello! Please take a look at the example. >>> a = # Just a list of tuples >>> a Now i want to get a list of functions x*y/n, for each (x, y) in a:
267
10502
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
23
5286
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
21
1822
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
5
166
by: I V | last post by:
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: With Ivan's approach, you lose access to the actual lambdas, so you need to create a new function and then modify its code object to...
11
1365
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.