473,414 Members | 1,946 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,414 software developers and data experts.

Problem in access the dictionary variable values,when passed as argument

440 256MB
Hi ,

The below snippet of the code is returing the empty dict,but I would like to get the values in the dict as {1:200,2:300,3:400}.

Could anybody suggest what method I have to use to return the dict values as function argument.

Expand|Select|Wrap|Line Numbers
  1. Sample.py
  2.  
  3. class Sample:
  4.     def __init__(self):
  5.         pass
  6.     def Func2(self):
  7.         dataDict2 = {1:200,2:300,3:400}
  8.         return dataDict2
  9.  
  10.     def Func1(self,dataDict):
  11.         dataDict = self.Func2()
  12. if __name__ == '__main__':
  13.  
  14.     dataDict = {}   
  15.     objSample = Sample()
  16.     objSample.Func1(dataDict)
  17.     print dataDict
  18.  
  19.  
Thanks
PSB
Aug 15 '07 #1
4 1578
ilikepython
844 Expert 512MB
Hi ,

The below snippet of the code is returing the empty dict,but I would like to get the values in the dict as {1:200,2:300,3:400}.

Could anybody suggest what method I have to use to return the dict values as function argument.

Expand|Select|Wrap|Line Numbers
  1. Sample.py
  2.  
  3. class Sample:
  4.     def __init__(self):
  5.         pass
  6.     def Func2(self):
  7.         dataDict2 = {1:200,2:300,3:400}
  8.         return dataDict2
  9.  
  10.     def Func1(self,dataDict):
  11.         dataDict = self.Func2()
  12. if __name__ == '__main__':
  13.  
  14.     dataDict = {}   
  15.     objSample = Sample()
  16.     objSample.Func1(dataDict)
  17.     print dataDict
  18.  
  19.  
Thanks
PSB
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. class Sample:
  2.     def __init__(self):
  3.         self.dd = {} 
  4.  
  5.     def func2(self):
  6.         self.dd = {1 : 200, 2 : 300, 3 : 400}
  7.  
  8.     def func1(self):
  9.         return self.dd
  10.  
  11. obj = Sample();
  12. obj.func2()
  13. dd = obj.func1()
  14.  
Aug 15 '07 #2
bartonc
6,596 Expert 4TB
Update the dict rather than reassign (much like aList.append()):
Expand|Select|Wrap|Line Numbers
  1. class Sample:
  2.     def __init__(self):
  3.         pass
  4.     def Func2(self):
  5.         dataDict2 = {1:200,2:300,3:400}
  6.         return dataDict2
  7.  
  8.     def Func1(self, dd):
  9.         dd.update(self.Func2())
  10. if __name__ == '__main__':
  11.  
  12.     dataDict = {}   
  13.     objSample = Sample()
  14.     objSample.Func1(dataDict)
  15.     print dataDict
Aug 15 '07 #3
psbasha
440 256MB
Update the dict rather than reassign (much like aList.append()):
Expand|Select|Wrap|Line Numbers
  1. class Sample:
  2.     def __init__(self):
  3.         pass
  4.     def Func2(self):
  5.         dataDict2 = {1:200,2:300,3:400}
  6.         return dataDict2
  7.  
  8.     def Func1(self, dd):
  9.         dd.update(self.Func2())
  10. if __name__ == '__main__':
  11.  
  12.     dataDict = {}   
  13.     objSample = Sample()
  14.     objSample.Func1(dataDict)
  15.     print dataDict
Thanks for the idea.But I need to pass the dict variable as argument for different methods of a class/classes.So it possible to send the dict variable as argument and get it back with the modified or filled elements in the dict variable

Thanks
PSB
Aug 15 '07 #4
bartonc
6,596 Expert 4TB
Thanks for the idea.But I need to pass the dict variable as argument for different methods of a class/classes.So it possible to send the dict variable as argument and get it back with the modified or filled elements in the dict variable

Thanks
PSB
It's pass-by-reference so the update thing work the same as
Expand|Select|Wrap|Line Numbers
  1. dd[key] = value
and vise versa. I was simply trying to keep your structure in tact.
Aug 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
5
by: Martin Chen | last post by:
I have a frame set (as per MS FrontPage 2000). It has a contents and a main frame. The contents frame has a menu bar written with with javascript (in the context of a table). In IE6.1 everything...
3
by: RubenDV | last post by:
I am writing code for a dictionary type in c, but i have some problems allocating the memory for it. The dictionary type itself is a struct with the following contents: typedef struct { char **...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
5
by: H J van Rooyen | last post by:
Hi, I am struggling to get the pack method to do what I intend. I am trying to display user input in a seperate window, along with a little description of the field, something like this: ...
4
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my...
4
by: Kev | last post by:
Hello, I have an Access 2003 database running on an XP network. I have a datasheet subform containing a 28 day roster - shift1 to shift28. Each record has 1 RosterEmpID, 1 EmployeeNumber, 28...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...
0
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...

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.