472,119 Members | 1,003 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Need to generate some functions.

Given a list of names

ll = ("n1", "n2", "n3", "n4")

I want to create a pair of functions based off of each name. An example of
what I want to happen would look like this:

def mkn1dict(address):
return {'Address': address, 'Control': SOME_CONST}

def mkn1Classobj(address):
return Classobj( mkn1classobj(address) )

I know how to do this in a preprocessor, but I'd like to do it directly in
python. Can this be done?

TIA
--
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 17 '07 #1
1 1066
you can use the dictionary returned by the built in function vars, along
the lines of
>>vars()["foo"] = lambda x: 3*x
foo(2)
6

but polluting your name space with data counts as bad style and will
probably bite you at some point -- you probably are better of putting
closures in a dictionary:
>>def mkdict(address):
def something():
return {'Address': address, 'Control': "SOME_CONST"}
return something
>>mk = {}
mk["n1"] = mkdict("n1")
mk["n1"]()
{'Control': 'SOME_CONST', 'Address': 'n1'}


Steven W. Orr wrote:
Given a list of names

ll = ("n1", "n2", "n3", "n4")

I want to create a pair of functions based off of each name. An example
of what I want to happen would look like this:

def mkn1dict(address):
return {'Address': address, 'Control': SOME_CONST}

def mkn1Classobj(address):
return Classobj( mkn1classobj(address) )

I know how to do this in a preprocessor, but I'd like to do it directly
in python. Can this be done?

TIA

Aug 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by wandaring | last post: by
1 post views Thread by alex bazan | last post: by
45 posts views Thread by Joh | last post: by
3 posts views Thread by visual basic dummy | last post: by
13 posts views Thread by Brett Baisley | last post: by
5 posts views Thread by HotRod | last post: by
reply views Thread by ward | last post: by
9 posts views Thread by pereges | last post: by

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.