How do I pass dict as parameterlist in Python 1.52 ?

Cromwell, Jeremy jcromwell at ciena.com
Fri Jan 11 13:25:31 EST 2002


Still true in 2.2, but it's not the error you think it is.

>>> def f(**kwargs):
... 	print kwargs
... 	
>>> d = {1:2, 3:4}
>>> f(**d)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: f() keywords must be strings
>>> d = {"one":2, "three":4}
>>> f(**d)
{'one': 2, 'three': 4}

Keywords come in as variable (parameter) names, so they're filtered as such.

>>> def f(1, 2):
Traceback (  File "<interactive input>", line 1
    def f(1, 2):
          ^
SyntaxError: invalid syntax
>>> def f(1=2, 3=4):
Traceback (  File "<interactive input>", line 1
    def f(1=2, 3=4):
          ^
SyntaxError: invalid syntax
>>> 

> **********************************************************
> Jeremy Cromwell
> CIENA Core Switching Division
> jcromwell at ciena.com
> 


-----Original Message-----
From: Hans Nowak [mailto:wurmy at earthlink.net]
Sent: Friday, January 11, 2002 9:56 AM
To: python-list at python.org
Subject: Re: How do I pass dict as parameterlist in Python 1.52 ?


Neal Norwitz wrote:
> 
> maxm wrote:
> >
> > I am writing a product in Zope, and it works fine on Zope 2.4.3 (Python
2.1)
> > where I developed it. Now I am testing it on Zope 2.3.3 (Python 1.5.2)
To be
> > backwards compatible, but When I start the server I get an error:
> >
> > manage_addots_UserForm = HTMLFile('manage_addots_UserForm',
> >         globals(), **{'formatTypes':ots_User.formatTypes})
> >                    ^
> >  SyntaxError: invalid syntax

Is there a reason why you can do this:

>>> g(*t)
>>> def g(*args):
	print args
>>> t = (4, 5, 6)
>>> g(*t)
(4, 5, 6)

but not this:

>>> def f(**kwargs):
	print kwargs
>>> d = {1:2, 3:4}
>>> f(**d)
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in ?
    f(**d)
TypeError: f() keywords must be strings

...anyone?

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list