Redo: How to create a function dynamically.

S

Steven W. Orr

I have this which works:

#! /usr/bin/python
strfunc = """
def foo( a ):
print 'a = ', a
"""
exec strfunc
globals()['foo'] = foo
foo( 'Hello' )

and this which does not:

#! /usr/bin/python
import new
strfunc = """
def foo( a ):
print 'a = ', a
"""
co = compile ( strfunc, '', 'exec' )
exec co
nfunc = new.function( co, globals(), 'foo' )
globals()['foo'] = nfunc
foo( 'Hello' )

When I try to run this one I get:
Traceback (most recent call last):
File "./m2.py", line 13, in ?
foo( 'Hello' )
TypeError: ?() takes no arguments (1 given)


I need the second case to work because I want to be able to end up with a
function in a seperate module to do the work of function creation. The
caller will pass in globals() so that the resulting function will end up
in the directory?/dictionary? of that caller.

[And my apologies for overcomplicating the question on my first try.]

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
P

Peter Otten

Steven said:
I have this which works:

#! /usr/bin/python
strfunc = """
def foo( a ):
print 'a = ', a
"""
exec strfunc
globals()['foo'] = foo
foo( 'Hello' )

and this which does not:

#! /usr/bin/python
import new
strfunc = """
def foo( a ):
print 'a = ', a
"""
co = compile ( strfunc, '', 'exec' )
exec co
nfunc = new.function( co, globals(), 'foo' )
globals()['foo'] = nfunc
foo( 'Hello' )

When I try to run this one I get:
Traceback (most recent call last):
File "./m2.py", line 13, in ?
foo( 'Hello' )
TypeError: ?() takes no arguments (1 given)


I need the second case to work because I want to be able to end up with a
function in a seperate module to do the work of function creation. The
caller will pass in globals() so that the resulting function will end up
in the directory?/dictionary? of that caller.

'co' is the code that creates the function foo(), not the code that is
executed when foo() is invoked.
Here's the brute force fix to make your script run without error:

#! /usr/bin/python
import new
strfunc = """
def foo( a ):
print 'a = ', a
"""
co = compile ( strfunc, '', 'exec' )
ns = {}
exec co in ns
nfunc = new.function(ns["foo"].func_code, globals(), 'foo' )
globals()['foo'] = nfunc
foo( 'Hello' )

I still don't understand what you're trying to do...

I'm in a nitpicky mood, so
(1) No need to create a new thread for this post.
(2)
http://www.googlefight.com/index.php?lang=en_GB&word1=seperate&word2=separate

Peter
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top