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

Steve Holden steve at holdenweb.com
Mon Jan 4 18:12:03 EST 2010


Nav wrote:
> On Jan 4, 4:54 pm, Chris Rebert <c... at rebertia.com> wrote:
>> On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik <sh... at milochik.com> wrote:
>>> You could put them in a dictionary with the key being the name, instead of a list.
>> To illustrate that for the OP:
>>
>> name2drink = {}
>> for booze in liquors:
>>     for juice in juices:
>>         name = juice +" "+booze # or however you're naming them
>>         drink = Bottle(booze, juice)
>>         name2drink[name] = drink
>>
>> #example use
>> favorite = name2drink["apple wine"]
>> favorite.rating = 9/10
> 
> typing
> favorite = such and such is what I am trying to avoid.
> 
> I want to be able to use the name 'apple_wine' as the variable which
> has the object apple wine but not have to do this manually as you did
> with favorite.
> 
Why? The example is trying to show you that it's much more sensible
(i.e. better controlled, easier to manage, less likely to cause
problems) if instead of looking up the names in the global namespace you
instead looked them up in a dictionary.

This question arises so frequently it should really be a FAQ, but the
closest I can come is

  http://techblog.ironfroggy.com/2007/06/dynamic-hell.html

which does at least exercise the necessary arguments.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list