function factories and argument names

Chadrik chadrik at gmail.com
Fri Jun 20 20:07:14 EDT 2008


i know how to programmatically create python functions:


module = __import__( '__main__')
def factory( val ):
	def f( arg=val ): print arg, val
	f.__name__ = 'function%s' % x
	return f

for x in [1,2,3,4]:
	func = factory( x )
	setattr( module, func.__name__, func)


but i'd like to add a little more customization: i'd like to vary the
name of the argument to the function, which is now fixed as 'arg':

help( function2 )

# Help on function function2 in module __main__:
#
# function2(arg=2)

ideally i'd like to create each function in the example with the given
format:

function1(arg1=1)
function2(arg2=2)
function3(arg3=3)
function4(arg4=4)

i know that the function will behave the same regardless of the arg
name, but i would like to help convey to the user what the meaning of
the argumetns are.  plus, i would also like to vary the number are
keyword args.

thanks in advance,
chad








More information about the Python-list mailing list