By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,502 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

Redo: How to create a function dynamically.

P: n/a
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
Aug 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Steven W. Orr wrote:
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...word2=separate

Peter
Aug 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.