471,337 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 1471
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

Post your reply

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

Similar topics

23 posts views Thread by Russ Chinoy | last post: by
2 posts views Thread by ajikoe | last post: by
7 posts views Thread by David Bear | last post: by
5 posts views Thread by Martin Chen | last post: by
3 posts views Thread by RubenDV | last post: by
39 posts views Thread by Martin Jørgensen | last post: by
5 posts views Thread by H J van Rooyen | last post: by
4 posts views Thread by Betina Andersen | last post: by

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.