Newbie: How to pass a dictionary to a function?

BonusOnus Bonus.Onus at gmail.com
Mon Apr 7 23:54:09 EDT 2008


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?



More information about the Python-list mailing list