473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Function return a dictionary

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
Oct 1 '07 #1
2 28970
Boris Mok <bo******@iwow.com.sgwrites:
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
Oct 1 '07 #2
Boris Mok <bo******@iwow.com.sgwrote:
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
Oct 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: david | last post by:
Hi All, I just want to write a function return char array,but I cant. I am not good at C++ 's concept since I used to use Java before. Would you please help me to fix that ? Thanks.. Best...
4
by: wongjoekmeu | last post by:
Hello All, I know that when you pass an argument to a function (if you want let the function be able to change the value) then you can choose to pass the argument by reference or a pointer to...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
7
by: chellappa | last post by:
hi this program return value automatically ... how it is possible ..i am not return any value... but i return correct values i am using Linux -gcc complier please tell me what is this main() {...
8
by: bdobby | last post by:
Hi. I am relatively new to js, but I did think I was starting to get the hang of it. Then this happened... I have a form with an onsubmit event handler: <form id="uploadForm" method="post"...
9
by: mtczx232 | last post by:
it's posible that Function return byref? what the syntax?
1
by: holdingbe | last post by:
Hi all, Normally, the stored function return one values per time.In oracle9i pipelined functions return the amount of data at a time, the first step create a type object. CREATE OR...
6
by: John | last post by:
Hi I have a function that needs to return value from some column in a table. The column type is not known in advance. Is it possible for function to return a value whose type would be determined...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
10
by: AlFire | last post by:
Hi, I am seeking an explanation for following: Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41) on linux2 Type "help", "copyright", "credits" or "license" for more information. .... {}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.