Newbie help- Can multiple instances with multiple names automatically created.

Steven D'Aprano steven at
Tue Jan 5 07:03:28 CET 2010

On Mon, 04 Jan 2010 21:27:12 -0800, Nav wrote:

> @ Steven....
> "No, you're confused -- the problem isn't with using the global
> namespace.
> The problem is that you don't know what names you want to use ahead of
> time. "
> Actually I know what the names would be and how I want to use them. 

You said earlier:

"I have a class of let's say empty bottle which can have a mix of two
items. I want to create let's say 30 of these objects which will have
names based on the 2 attributes (apple juice, beer, grape juice, beer,
etc) that I provide from a list."

Your description is confusing to me. What on earth is an empty bottle 
which has a mix of two items in it? Surely that means it's not empty any 
more? But putting that aside:

"All the objects are a mix of (1 of three alcohols) and (1 of  10 
juices), so I don't want to go through typing in the names of all the 
objects (which would be totally stupid)."

Right... so your problem isn't that you don't know what the variable 
names is, but there are too many to comfortably enumerate in the source 

The answer is, again, avoid named variables. Instead of (say):

gin_apple_strawberry = Bottle('gin', 'apple')
gin_apple_orange  = Bottle('gin', 'orange')
# etc.

again you should use a list or dict:

bottles = []
for alcohol in ('gin', 'beer', 'wine'):
    for fruit in ('apple', 'banana', 'blueberry', 
    'strawberry', 'orange', 'peach'):
            bottles.append(Bottle(alcohol, fruit))
for bottle in bottles:


More information about the Python-list mailing list