[Tutor] understanding **kwargs syntax

John washakie at gmail.com
Thu Sep 8 12:10:48 CEST 2011


Following up on this...

Why does pylint seem to think it is a bad idea?

Description	Resource	Path	Location	Type
ID:W0142 plot_ts_array_split_x: Used * or **
magic	tsplot.py	/research.plot	line 299	PyLint Problem

Thanks,
john


On Thu, Aug 25, 2011 at 10:44 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 25/08/11 09:27, John wrote:
>>
>> Just a quick question,
>>
>> is it wrong to use the *args and **kwargs ( the latter in particular)
>> when DEFINING a function?
>
> No, in fact it's essential if you don't know in advance what arguments are
> going to be passed to the function. This is very common if you are wrapping
> another function that itself takes variable arguments.
>
>> def fest(**kwargs):
>>     """ a function test """
>>     keys = sorted(kwargs.keys())
>>
>>     print("You provided {0} keywords::\n".format(len(keys)))
>>     for kw in keys:
>>         print("{0} =>  {1}".format(kw, kwargs[kw]))
>>
>> Are there some good examples of when this would be a good idea to
>> implement?
>
> GUI frameworks like Tkinter often take variable numbers of configuration
> values. If you want to wrap that with a
> function of your own you can do something like(pseudocode):
>
> def myfunc(**kwargs):
>   if some interesting arg exists
>        do something with it
>   return wrappedFunc(kwargs)
>
> There are plenty other cases, try grepping the standard library code
> for lines with def and **kwargs for some examples...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Configuration
``````````````````````````
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Python 2.6
PIL 1.1.6
Mailman 2.1.9
Postfix 2.4.5
Procmail v3.22 2001/09/10
Basemap: 1.0
Matplotlib: 1.0.0


More information about the Tutor mailing list