**kwargs

Gerrit Holl gerrit.holl at pobox.com
Tue Jan 18 16:58:26 EST 2000


sp00fD wrote on 948224316:
> Does **kwargs represent a dictionary?  If so, can I have a method like
> such
> 
> def foo(self, foo=None, bar=None, **kwargs)
> 
> and then do something like
> 
> this_dict = {}
> ..insert stuff into this_dict..
> 
> my.foo(foo="something", bar="something else", this_dict)
> 
> ? How would I do something like this ?

Use apply:

>>> def f(foo=None, bar=None, **kwargs):
...     print "foo is: %s" % foo
...     print "bar is: %s" % bar
...     print "kwargs is: %s" % kwargs
...
>>> f(foo="something", bar="something else", fun="cool")
foo is: something
bar is: something else
kwargs is: {'fun': 'cool'}
>>> apply(f, (), {"foo": "something", "bar": "something else", "fun": "cool"})
foo is: something
bar is: something else
kwargs is: {'fun': 'cool'}
>>> print apply.__doc__
apply(function, args[, kwargs]) -> value

Call a function with positional arguments taken from the tuple args,
and keyword arguments taken from the optional dictionary kwargs.

Library reference:
   apply (function, args[, keywords])
          The function argument must be a callable object (a user-defined
          or built-in function or method, or a class object) and the args
          argument must be a sequence (if it is not a tuple, the sequence
          is first converted to a tuple). The function is called with
          args as the argument list; the number of arguments is the the
          length of the tuple. (This is different from just calling
          func(args), since in that case there is always exactly one
          argument.) If the optional keywords argument is present, it
          must be a dictionary whose keys are strings. It specifies
          keyword arguments to be added to the end of the the argument
          list.

regards,
Gerrit.

-- 
Please correct any bad English you encounter in my email message!
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK-----




More information about the Python-list mailing list