[Tutor] retrieving instances value
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 7 Oct 2002 17:30:43 +0100
> class Transaction:
> def __init__(self,name='',address='',age=''):
> self._name = name
> self._address = address
> self._age = age
>
> # make a Transaction instance
> obj = Transaction('my name', 'my address', 25)
>
> how can i retrieve each 'obj' instance value without typing it one by
> one manually (in case it have 100 instances)?
Store the instances in a list or dictionary.
Probably a dictionary keyed by name, or maybe even by
the tuple of value.
So:
XnList = []
XnDict = {}
while 1:
name = raw_input("NAME? ")
addr = raw_input("ADDRESS? ")
ag = raw_input("AGE? ")
obj = Transaction(name,addr,age)
XnDict[n] = obj
# OR ... XnDict[(name,addr,age)] = obj
# OR ... XnList.append(obj)
And access them with:
for x in XnDict.keys(): print XnDict[x].name
OR
for x in XnList: print x.name
HTH,
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld