Python Cookbook dict problem

Dave Reed dreed at capital.edu
Wed Jul 31 09:18:51 EDT 2002


> From: Aldo Cortesi <aldo at nullcube.com>
> 
> Thus spake Dave Reed (dreed at capital.edu):
> 
> > 
> > I'm trying the following Python Cookbook recipe with python2.2
> > 
> > 
> > def makedict(**kwargs):
> >     return kwargs
> > data = makedict(red=1, green=2, blue=3)
> > def dodict(*args, **kwds):
> >     d = {}
> >     for k, v in args: d[k] = v
> >     d.update(kwds)
> >     return d
> > tada = dodict(*data.items(), yellow=2, green=4)
> > 
> > 
> > but I get an error with the last line:
> > >>> tada = dodict(*data.items(), yellow=2, green=4)
> >   File "<stdin>", line 1
> >     tada = dodict(*data.items(), yellow=2, green=4)
> >                                       ^
> > SyntaxError: invalid syntax
> > 
> > What's wrong?
> 
> 
> Dave,
> 
> 
> Python expects you to do positional and keyword argument
> unrolling at the end of the function argument list (i.e.
> after all "normal" arguments). Change the last line to:  
> 
> tada = dodict(yellow=2, green=4, *data.items())


Thanks. Did something change in Python versions or is it a typo in the
book?

Dave




More information about the Python-list mailing list