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

Variable Pointer to Dynamically Changing List Entry

P: 1
Hi,

I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession

Expand|Select|Wrap|Line Numbers
  1.  
  2. myChange = [0, 0, 0, 1]
  3.  
where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents-

Expand|Select|Wrap|Line Numbers
  1. myChange [3] = myChange [3] - 1
  2. myChange [2] = myChange [2] + 2
  3.  
I can say

Expand|Select|Wrap|Line Numbers
  1. print quarters
  2. print dimes
  3.  
And get the following output?

0
2

Thanks for the help; sorry if my terminology is bad as I am new to programming!
Jul 30 '07 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
Hi,

I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession

Expand|Select|Wrap|Line Numbers
  1.  
  2. myChange = [0, 0, 0, 1]
  3.  
where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents-

Expand|Select|Wrap|Line Numbers
  1. myChange [3] = myChange [3] - 1
  2. myChange [2] = myChange [2] + 2
  3.  
I can say

Expand|Select|Wrap|Line Numbers
  1. print quarters
  2. print dimes
  3.  
And get the following output?

0
2

Thanks for the help; sorry if my terminology is bad as I am new to programming!
Your terminology (and use of this site) is excellent for a beginner!

You could achive 100% of your goal (getting a reference to an array item) using an extension called scipy. A 99% solution with native lists requires that you unpack the list each time you want to use it. It's a technique that I use often:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [1, 2, 3, 4]
  2. >>> pennies, nickles, dimes, quarters = aList
  3. >>> quarters
  4. 4
  5. >>> 
Jul 30 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
You may consider using a class for your program. This is a start:
Expand|Select|Wrap|Line Numbers
  1. class myChange(object):
  2.  
  3.     def __init__(self, penny=0, nickel=0, dime=0, quarter=0):
  4.         self.penny = penny
  5.         self.nickel = nickel
  6.         self.dime = dime
  7.         self.quarter = quarter
  8.         self.data = [penny,nickel,dime,quarter]
  9.         self.__init = 1
  10.  
  11.     def __getitem__(self, i):
  12.         return self.data[i]        
  13.  
  14.     def __setitem__(self, i, value):
  15.         try:
  16.             object.__setattr__(self, {0:'penny',1:'nickel',2:'dime',3:'quarter'}[i], int(value))
  17.             object.__setattr__(self, 'data', [int(self.penny), int(self.nickel), int(self.dime), int(self.quarter)])
  18.         except KeyError:
  19.             raise IndexError, "Index argument out of range in '%s' __setitem__" % (type(self).__name__)
  20.         except ValueError:
  21.             raise ValueError, "Invalid literal in '%s' __setitem__" % (type(self).__name__)
  22.  
  23.     def __setattr__(self, name, value):
  24.         if not self.__dict__.has_key('_myChange__init'):
  25.             return object.__setattr__(self, name, value)
  26.         elif self.__dict__.has_key(name):
  27.             object.__setattr__(self, name, value)
  28.             object.__setattr__(self, 'data', [int(self.penny), int(self.nickel), int(self.dime), int(self.quarter)])
  29.         else:
  30.             raise AttributeError, "'%s' object has no attribute '%s'" % (type(self).__name__, name)
Here is some interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> myChange(6,4,7,2)
  2. <__main__.myChange object at 0x00F972D0>
  3. >>> chg = myChange(6,4,7,2)
  4. >>> chg.penny
  5. 6
  6. >>> chg.penny = 12
  7. >>> chg.data
  8. [12, 4, 7, 2]
  9. >>> chg.quarter += 7
  10. >>> chg.quarter
  11. 9
  12. >>> chg.data
  13. [12, 4, 7, 9]
  14. >>> chg[3]=3
  15. >>> chg.quarter
  16. 3
  17. >>> chg[3]
  18. 3
  19. >>> chg[4]
  20. Traceback (most recent call last):
  21.   File "<interactive input>", line 1, in ?
  22.   File "C:\SDS2_7.0\macro\Work In Progress\myChange.py", line 13, in __getitem__
  23.     return self.data[i]
  24. IndexError: list index out of range
  25. >>> 
Jul 31 '07 #3

Post your reply

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