<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">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.<br><br>###START<br><br>def function_with_kwargs(a, b, **kwargs):<br> options = {<br> 'logfile' : None,<br> 'door' : 'kitchendoor',<br> 'roof' : 'tiles',<br> 'mydict' : None,<br> 'mylist' : None, }<br> options.update(kwargs)<br><br> logfile =
options.get('logfile')<br> if logfile == None:<br> print "No logging"<br> else:<br> print "Logging"<br><br> mydict = options.get('mydict')<br> if mydict == None:<br> print "Do nothing with dictionary"<br> else:<br> print "Do something with dictionary"<br><br> mylist = options.get('mylist')<br> if mylist == None:<br> print "Do nothing with list"<br> else:<br> print "Do something with list"<br><br> print "END OF FUNCTION\n"<br> <br><br>somedict = { 'a': '1', 'b': '2', }<br>somelist = ['1', '2']<br><br>#DO
SOMETHING<br>function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', mydict=somedict, mylist=somelist)<br><br>#DO NOTHING<br>function_with_kwargs(1, 2, door='frontdoor')<br><br><br><br>### END<br><br>I have 2 questions about this code:<br><br>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)<br><br>"At this writing, both apply and the special call syntax described in this<br>section can be used freely in Python 2.5, but it seems likely that apply<br>may go away in Python 3.0. If you wish to future-proof your code, use<br>the equivalent special call syntax, not apply."<br><br>2. Are there better ways to achieve what I want to do?<br><br><br><br><br><br><br><br></td></tr></table><br>