By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

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

100+
P: 440
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
Share this Question
Share on Google+
4 Replies


ilikepython
Expert 100+
P: 844
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
Expert 5K+
P: 6,596
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

100+
P: 440
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
Expert 5K+
P: 6,596
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

Post your reply

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