473,372 Members | 1,067 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,372 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 1577
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.