Function return a dictionary

Duncan Booth duncan.booth at invalid.invalid
Mon Oct 1 10:05:47 CEST 2007


Boris Mok <borismok at iwow.com.sg> wrote:

> Hi all,
> 
> I'm doing a function which needs return an arrary -- or more specially a 
> dictionary data type.
> I have a sample like this
> 
> def AFC():
>   v["a"] = 1
>   return v
> 
> v = AFC()
> print v["a"]
> 
> with error
> NameError: global name 'v' is not defined
> 
> how do I go about it?
> 
At some point you have to initialise v to a dictionary, Python isn't going 
to guess for you. e.g.

def AFC():
  v = {}
  v["a"] = 1
  return v



More information about the Python-list mailing list