function factories and argument names

C

Chadrik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top