[Tutor] OOP Newbie Q
Gregor Lingl
glingl@aon.at
Mon, 1 Jul 2002 22:31:00 +0200
----- Original Message -----
From: "Alan Colburn" <aicolburn@yahoo.com>
To: <tutor@python.org>
> ... How can you create a new
> object without having to manually enter the source
> code each time?
>
> a=BankAccount()
> b=BankAccount()
> c=BankAccount()
>
> This would get a bit tedious after awhile! ...
> ... code like this wouldn't work for creating a new
> BankAccount object:
>
> acctName=raw_input("Account name? ")
> acctName=BankAccount()
>
> Any thoughts? Thanks! -- Al C.
>
If you have to store an unknown number of objects
of any kind, you should think of using a compound data type
to store them, as are, fo instance lists or - maybe
in your case this would serve better - dictionaries:
accounts = {}
while < some condition > :
acctName=raw_input("Account name? ")
accounts[acctName] = BankAccount()
....
so you could retrieve any account via
its name this way:
actualAccount = accounts[acctName]
...
Gregor