473,796 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating anonymous functions using eval

I've been playing with a function that creates an anonymous function by
compiling a string parameter, and it seems to work pretty well:

def fn( text):
exec 'def foo' + text.strip()
return foo

This can be used like:

def foo( x):
print x( 2, 5)

foo( fn( '''
( x, y):
print 'x^2 + y^2 = ', x*x + y*y
return y
'''))

- which outputs:

x^2 + y^2 = 29
5
You can also mimic conventional function definitions:

f = fn( '''
( x, y):
print 'x, y=', x, y
print 1.0*x/y
return y
''')

print 'calling f'
f( 5, 6)

This outputs:

calling f
x, y= 5 6
0.833333333333
You do things like allow/require an initial `def' for clarity, check that all
the lines of text are indented so that nothing is executed when the anonymous
function is created, etc etc.

Obvious disadvantages are that the function text may have to escape quotes,
and will not be showed with syntax colouring in editors.

Apart from that, it seems quite a useful way of overcoming the limitations of
python's lambda.

But... I haven't seen this technique mentioned anywhere, so does anyone know
of any problems that I should be wary of?

- Julian

--
http://www.op59.net/
Jul 21 '05
10 2405
On 12 Jul 2005 08:28:45 -0700
"Devan L" <de****@gmail.c om> wrote:

[ here's some context:
I've been playing with a function that creates an anonymous function by
compiling a string parameter, and it seems to work pretty well:

def fn( text):
exec 'def foo' + text.strip()
return foo

This can be used like:

def foo( x):
print x( 2, 5)

foo( fn( '''
( x, y):
print 'x^2 + y^2 = ', x*x + y*y
return y
'''))

- which outputs:

x^2 + y^2 = 29
5
]
How is this different from a nested function?


It's different because it is defined in an expression, not by a
statement. So you can create a new function inside a function call, for
example. Like I said in the original post, it's a way of overcoming the
limitations of python's lambda.

The simple version I posted fails to lookup things in the caller's context,
which one can easily fix by passing locals() as an extra parameter. Hopefully
it'll be possible to grab the caller's locals() automatically using traceback
or similar.

Here's a version I've been using for a while which seems to work pretty
well:

def fn( text, globals_={}, locals_={}):
'''
Returns an anonymous function created by calling exec on <text>.
<text> should be a function definition, ommiting the initial
`def <fnname>' (actually, leading `def' can be retained, for
clarity). Any leading/trailing white space is removed using
str.strip(). Leading white space on all lines will be handled
automatically by exec, so you can use an indented python
triple-quoted string.

In addition, newlines and backslashes are re-escaped inside
single/double-quoted strings. this enables nested use of fn().

example usage:

fn(
"""
def ( target, state):
if target != 'foo': return None
return [], None, 'touch foo'
""", globals(), locals())

Would be nice to grab our caller's globals() and locals() if
globals_/locals_ are None, using some sort of introspection. For
now, you'll have to pass globals() and locals() explicitly.
'''
text = text.strip()
if text.startswith ( 'def'): text = text[ 3:].strip()
if not text.startswith ( '('):
raise Exception( 'fn string must start with `(\'')

def _escape_quotes( text):
'''
escape newlines and backslashes that are inside
single/double-quoted strings. should probably do something about
backslashes inside triple-quoted strings, but we don't bother
for now.
'''
quote = None
ret = ''
for c in text:
if quote:
if c=='\n':
ret += '\\n'
elif c=='\\':
ret += '\\\\'
else:
if c==quote:
quote = None
ret += c
else:
if c=='\'' or c=='"':
quote = c
ret += c
return ret
text = _escape_quotes( text)

#print 'execing:', text

# the exec will put the fn in the locals_ dict. we return it after
# removing it from this dict.
exec 'def _yabs_fn_temp' + text in globals_, locals_
ret = locals_['_yabs_fn_temp']
del locals_['_yabs_fn_temp']
return ret
- Julian

--
http://www.op59.net/
Aug 22 '05 #11

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

Similar topics

0
1176
by: Andrew Collier | last post by:
Hello, I was writing a program which used some nested functions, and came across a behaviour which I was unable to explain. I can summarise it with the example below: #!/usr/bin/env python
2
2484
by: Salvatore | last post by:
Hello, Given an expression f = '2*x+3' from a user input I would like to do some sort of function userFunction(x): return f(x) is it possible with Javascript ?
20
3127
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then alert(a2) I will see 42 there too. I'm doubting, but I was
8
2560
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
0
1126
by: Andrew Westgarth | last post by:
Hi all, i'm struggling with a page idea I have. I need to write a page with an A to Z list of available schools in the area. I only want to display the letters in the A to Z which have school information under them, i.e. A,b,c,d,f if no schools beginning with e are available. My chosen page layout out would be
60
5472
by: jacob navia | last post by:
Gnu C features some interesting extensions, among others compound statements that return a value. For instance: ({ int y = foo(); int z; if (y>0) z = y; else z=-y; z; }) A block enclosed by braces can appear within parentheses to form a block that "returns" a value. This is handy in some macros, or in other applications.
11
7179
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
4
2130
by: Danny Shevitz | last post by:
Simple question here: I have a multiline string representing the body of a function. I have control over the string, so I can use either of the following: str = ''' print state return True '''
9
1877
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't associate the call with the handling routine (for reference purposes, I'm calling the Google Language Translation API). As a work-around, I thought I'd dynamically generate a unique callback function for each API call. Right now, I'm stuck hobbling...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
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.