Re: Dynamically pass a function arguments from a dict

Discussion in 'Python' started by Mark McEahern, Feb 24, 2005.

  1. Dan Eloff wrote:

    > How can you determine that func2 will only accept
    > bar and zoo, but not foo and call the function with
    > bar as an argument?


    Let Python answer the question for you:

    >>> def func2(bar='a', zoo='b'):

    .... pass
    ....
    >>> for name in dir(func2):

    .... print '%s: %s' % (name, getattr(func2, name))
    ....
    __call__: <method-wrapper object at 0x008CAC30>
    __class__: <type 'function'>
    __delattr__: <method-wrapper object at 0x008CAC30>
    __dict__: {}
    __doc__: None
    __get__: <method-wrapper object at 0x008CAC30>
    __getattribute__: <method-wrapper object at 0x008CAC30>
    __hash__: <method-wrapper object at 0x008CAC30>
    __init__: <method-wrapper object at 0x008CAC30>
    __module__: __main__
    __name__: func2
    __new__: <built-in method __new__ of type object at 0x1E0BAA50>
    __reduce__: <built-in method __reduce__ of function object at 0x008CE5B0>
    __reduce_ex__: <built-in method __reduce_ex__ of function object at
    0x008CE5B0>
    __repr__: <method-wrapper object at 0x008CAC30>
    __setattr__: <method-wrapper object at 0x008CAC30>
    __str__: <method-wrapper object at 0x008CAC30>
    func_closure: None
    func_code: <code object func2 at 008D5020, file "<stdin>", line 1>
    func_defaults: ('a', 'b')
    func_dict: {}
    func_doc: None
    func_globals: {'func2': <function func2 at 0x008CE5B0>, 'name':
    'func_globals',
    '__builtins__': <module '__builtin__' (built-in)>, '__name__':
    '__main__', 'foo
    : <function foo at 0x008CE970>, '__doc__': None}
    func_name: func2
    >>> for name in dir(func2.func_code):

    .... print '%s: %s' % (name, getattr(func2.func_code, name))
    ....
    __class__: <type 'code'>
    __cmp__: <method-wrapper object at 0x008CACF0>
    __delattr__: <method-wrapper object at 0x008CACF0>
    __doc__: code(argcount, nlocals, stacksize, flags, codestring,
    constants, names,

    varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])

    Create a code object. Not for the faint of heart.
    __getattribute__: <method-wrapper object at 0x008CACF0>
    __hash__: <method-wrapper object at 0x008CACF0>
    __init__: <method-wrapper object at 0x008CACF0>
    __new__: <built-in method __new__ of type object at 0x1E0B0C68>
    __reduce__: <built-in method __reduce__ of code object at 0x008D5020>
    __reduce_ex__: <built-in method __reduce_ex__ of code object at 0x008D5020>
    __repr__: <method-wrapper object at 0x008CACF0>
    __setattr__: <method-wrapper object at 0x008CACF0>
    __str__: <method-wrapper object at 0x008CACF0>
    co_argcount: 2
    co_cellvars: ()
    co_code: d S
    co_consts: (None,)
    co_filename: <stdin>
    co_firstlineno: 1
    co_flags: 67
    co_freevars: ()
    co_lnotab: ☺
    co_name: func2
    co_names: ()
    co_nlocals: 2
    co_stacksize: 1
    co_varnames: ('bar', 'zoo')
    >>>


    Hmm, func2.func_code.co_varnames seems to have the answer.

    Cheers,

    // m
     
    Mark McEahern, Feb 24, 2005
    #1
    1. Advertising

  2. Mark McEahern wrote:
    > Dan Eloff wrote:
    >> How can you determine that func2 will only accept
    >> bar and zoo, but not foo and call the function with
    >> bar as an argument?

    >
    > Let Python answer the question for you:
    >
    > ... <good explanation of how to do this for simple functions.>


    Please be aware the "normal" way to do this is go ahead and call
    the function. Many "function wrapping" techniques will fail this
    test, and often the code that looks into the guts of a call in
    order to do "something clever" will fail when it is pointed at
    anything that uses the technique.

    Not only does curry:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549

    not show you what args it accepts, but decorators, a Python 2.4
    invention, will typically obscure the interface of the decorated
    function. Since they wrap _any_ function call, they typically
    take the most general arguments possible in order to accommodate
    the widest range of functions to wrap.

    --Scott David Daniels
     
    Scott David Daniels, Feb 24, 2005
    #2
    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. Skip Montanaro
    Replies:
    0
    Views:
    426
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    371
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,341
    Kent Johnson
    Dec 15, 2006
  4. jmborr
    Replies:
    1
    Views:
    445
    Stargaming
    Nov 3, 2007
  5. John Gordon
    Replies:
    0
    Views:
    92
    John Gordon
    Mar 11, 2013
Loading...

Share This Page