[Tutor] Proper way to use **kwargs?

spir denis.spir at gmail.com
Mon Mar 15 20:25:20 CET 2010


On Mon, 15 Mar 2010 05:38:43 -0700 (PDT)
Karjer Jdfjdf <karper12345 at yahoo.com> wrote:

> I want to use **kwargs to check a list of conditions (if true do this, if false do nothing) besides required parameters ( in sample a and b).  Sometimes I want to add a Python object (in example a dictionary and a list). Below is my first **kwargs-brew.
> 
> ###START
> 
> def function_with_kwargs(a, b, **kwargs):
>     options = {
>         'logfile'   : None,
>         'door'      : 'kitchendoor',
>         'roof'      : 'tiles',
>         'mydict'    : None,
>         'mylist'    : None, }
>     options.update(kwargs)

The default set of options is a constant independant of the function and of its (other) arguments. So, I would rather define it outside. I find it clearer.

from copy import copy
OPTIONS = {
	'logfile'	: None,
	'door'		: 'kitchendoor',
	'roof'		: 'tiles',
	'mydict'	: None,
	'mylist'	: None,
}
def function_with_kwargs(a, b, **kwargs):
	options = copy(OPTIONS)
	options.update(kwargs)
	print options

(Must be copied to avoid the defaults to be altered by update.)

>     logfile = options.get('logfile')
>     if logfile == None:
>         print "No logging"
>     else:
>         print "Logging"

You don't need to use get, use options['logfile'] instead. Get is only useful if you use its default_value parameter. This would indeed be a good alternative to a global default dict for options:
	logfile = options.get('logfile', None)
	...
	roof = options.get('roof', 'tiles')
or maybe better the general pattern:
	x = options.get('x', OPTIONS['x'])

>     mydict = options.get('mydict')
>     if mydict == None:
>         print "Do nothing with dictionary"
>     else:
>         print "Do something with dictionary"
> 
>     mylist = options.get('mylist')
>     if mylist == None:
>         print "Do nothing with list"
>     else:
>         print "Do something with list"
> 
>     print "END OF FUNCTION\n"
>         
> 
> somedict = { 'a': '1', 'b': '2', }
> somelist = ['1', '2']
> 
> #DO SOMETHING
> function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', mydict=somedict, mylist=somelist)
> 
> #DO NOTHING
> function_with_kwargs(1, 2, door='frontdoor')
> 
> 
> 
> ### END
> 
> I have 2 questions about this code:
> 
> 1. Can I use this in Python 3 ?  I'm not sure if I can use **kwargs in Python 3 because it uses the "apply" builtin (if I understand it correctly)
> 
> "At this writing, both apply and the special call syntax described in this
> section can be used freely in Python 2.5, but it seems likely that apply
> may go away in Python 3.0. If you wish to future-proof your code, use
> the equivalent special call syntax, not apply."
> 
> 2. Are there better ways to achieve what I want to do?

In this case, I would use a general config object, which attributes are individual parameters. You need to first define a fake Config class (because for any reason we cannot alter direct instances of object).

Denis
________________________________

la vita e estrany

spir.wikidot.com



More information about the Tutor mailing list