Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Function return a dictionary

Reply
Thread Tools

Function return a dictionary

 
 
Boris Mok
Guest
Posts: n/a
 
      10-01-2007
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?

Thanks in advance.
Boris Mok
 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      10-01-2007
Boris Mok <(E-Mail Removed)> writes:

> I'm doing a function which needs return an arrary -- or more specially
> a dictionary data type.


Yes. Python doesn't have an "array" type natively, and it's confusing
to refer to a dict as an array because there *are* "array"s in PyNum.

> I have a sample like this
>
> def AFC():
> v["a"] = 1
> return v


Your function never specifies where 'v' comes from. So, when you first
attempt to access an item from 'v' as an existing dict, you get a
NameError.

If you want to create 'v' inside the function, you'll need to do so
before attempting to use it.

def foo():
bar = {"spam": 1}
return bar

cheeseburger = foo()
print cheeseburger["spam"]

To get a thorough grounding in basic concepts like this, please work
through the Python tutorial <URL:http://docs.python.org/tut/>, from
beginning to end, running every example and experimenting until you
understand why it does what it does, before moving onto the next.

--
\ "[T]he question of whether machines can think [...] is about as |
`\ relevant as the question of whether submarines can swim." |
_o__) —Edsger W. Dijkstra |
Ben Finney
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      10-01-2007
Boris Mok <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
return dictionary from a function JT ASP General 1 09-29-2003 05:54 PM



Advertisments