Newbie: How to pass a dictionary to a function?

Brian breily at gmail.com
Tue Apr 8 01:03:20 EDT 2008


Plus you probably don't want to set [] as default argument and then try to
access it like a dictionary; you'll get an exception if you ever call just
foo(), with no argument.

On Tue, Apr 8, 2008 at 12:57 AM, Jason Scheirer <jason.scheirer at gmail.com>
wrote:

> On Apr 7, 8:54 pm, BonusOnus <Bonus.O... at gmail.com> wrote:
> > How do I pass a dictionary to a function as an argument?
> >
> > # Say I have a function foo...
> > def foo (arg=[]):
> > x = arg['name']
> > y = arg['len']
> >
> > s = len (x)
> >
> > t = s + y
> >
> > return (s, t)
> >
> > # The dictionary:
> >
> > dict = {}
> > dict['name'] = 'Joe Shmoe'
> > dict['len'] = 44
> >
> > # I try to pass the dictionary as an argument to a
> > # function
> >
> > len, string = foo (dict)
> >
> > # This bombs with 'TypeError: unpack non-sequence'
> >
> > What am I doing wrong with the dictionary?
>
> You want to
> return s, t
> NOT return (s, t) -- this implicitly only returns ONE item
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080408/7b894535/attachment-0001.html>


More information about the Python-list mailing list