Need to generate some functions.
J. Robertson
jr244 at kent.ac.uk
Fri Aug 17 10:22:06 EDT 2007
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
>
>
More information about the Python-list
mailing list