473,808 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28990
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
15891
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 Regards, David
4
4290
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 that argument. Now my question is, why would you prefer to let a function return a reference or a pointer ??? Consider the following with operator overloading. I suppose the operator can be seen as a function (right ?)
8
14010
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 stack. this contains the arguments this function was called with, address to return to after return from this function (the location in the previous stack frame), location of previous frame on stack (base or start of this frame) and local variables...
7
1728
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() { int a,b,c,sum; printf("ENTER ANY THREE NUMBERS :\n"); scanf("%d%d%d",&a,&b,&c);
8
2078
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" action="..." onSubmit="checkDates()"> The event handler does some minor validation, then returns true or
9
11121
by: mtczx232 | last post by:
it's posible that Function return byref? what the syntax?
1
3445
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 REPLACE package My_Types is type lookup_row is record ( idx number, text varchar2(20) ); type lookups_tab is table of lookup_row; end My_Types;
6
1619
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 at runtime? If so, how? Thanks Regards
4
2486
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. Here's the approach I'm currently looking at. I throw exceptions only from constructors. Destructors, of course, do not throw exceptions. All other functions return a signed integer. The values are all stored in one large header file (as...
10
1053
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
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.