[Tutor] creating objects in a loop

Kirby Urner urnerk@qwest.net
Tue, 2 Apr 2002 23:34:18 -0500


On Tuesday 02 April 2002 10:41 pm, you wrote:
> On Tuesday, April 2, 2002, at 03:16  PM, Kirby Urner wrote:
> > Filing your multiple objects in a dictionary or list
> > is usually better than creating a lot of top level
> > variables in the namespace.
>
> Is this for tidiness reasons (to avoid cluttering the namespace with
> unnecessary names), or is this convention?
>
>
> Thank you,
>
> Erik

It gives you more control to store your objects in a dictionary.  
Then you can easily iterate through it, check the keys, pass 
the dictionary around among functions.  

If you stick everything in as a separately named global variable, 
then you're without the means to work with the pets as a group.
Sure, the globals are contained in a dictionary of their own, but 
the pets won't be distinguished from the globals.

At least one other respondant to this thread defined a new 
class to handle the pets, not just a dictionary.  This is certainly 
an option -- basically it depends on the application, and how 
much control you really need.

As per the Stocks example, which did the same thing as Pets, 
and BankAccounts (i.e. it stuffed individual stock instances 
into a dictionary), one advantage of having all these objects
grouped together is they're what you want to save to disk
at the end of the day -- so upon exiting, the dictionary passes 
all its contents off to shelve (a module), which updates a 
file, and everyone goes home happy.

Kirby