473,395 Members | 2,796 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,395 software developers and data experts.

Redo: How to create a function dynamically.

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
1 1482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lkrubner | last post by:
I'm offering users the ability to type weblog posts into a form and post them. They type the text into a TEXTAREA which is on a form. The form, when submitted, hits a PHP script. Before it is...
3
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
7
by: Peter Laan | last post by:
Is there a simple way to encapsulate the functionality to redo a method call a second time in case a specific exception is thrown? We are sending commands to an external system and if the sessionId...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.