function factories and argument names

Discussion in 'Python' started by Chadrik, Jun 21, 2008.

  1. Chadrik

    Chadrik Guest

    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
    Chadrik, Jun 21, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark F. Haigh
    Replies:
    1
    Views:
    329
    David Hilsee
    Aug 15, 2004
  2. Replies:
    1
    Views:
    445
    Oliver Wong
    Oct 2, 2006
  3. Reckoner
    Replies:
    11
    Views:
    698
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    558
    Seebs
    Nov 21, 2011
  5. Edward Rutherford

    Function factories

    Edward Rutherford, Aug 21, 2012, in forum: C Programming
    Replies:
    37
    Views:
    847
    Andrew Cooper
    Aug 25, 2012
Loading...

Share This Page