Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie help- Can multiple instances with multiple namesautomatically created.

Reply
Thread Tools

Newbie help- Can multiple instances with multiple namesautomatically created.

 
 
Nav
Guest
Posts: n/a
 
      01-04-2010
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. 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).
I get problems if I try such as I can't assign to literal etc.
If I make a list of names using the attributes. then equate names to
objects. the list gets populated by the objects and the names
disappear.
I want the ability to be able to call up any object by its name and
manipulate it and yet not have to assign the name manually. How can
this be done?

 
Reply With Quote
 
 
 
 
Shawn Milochik
Guest
Posts: n/a
 
      01-04-2010
You could put them in a dictionary with the key being the name, instead of a list.

Shawn
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-04-2010
On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik <(E-Mail Removed)> 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

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Jan Kaliszewski
Guest
Posts: n/a
 
      01-04-2010
2010-01-04, 22:54:41 Chris Rebert <(E-Mail Removed)> wrote:

> 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


@Nav: ...and if you really desire to have those objects in global
(module's) namespace, you can do that:

global_namespace = globals()
for booze in liquors:
for juice in juices:
name = '{0}_with_{1}_juice'.format(booze, juice)
drink = Bottle(booze, juice)
global_namespace[name] = drink

# then you have them in module's namespace
print(wine_with_apple)
wine_with_apple.rating = 0.9

Though in most cases it'd be not necessary.

Cheers,
*j

PS. Another way to express practically the same:

from itertools import product

globals().update(('{0}_with_{1}_juice'.format(booz e, juice),
Bottle(booze, juice))
for booze, juice in product(liquors, juices))

--
Jan Kaliszewski (zuo) <(E-Mail Removed)>
 
Reply With Quote
 
Nav
Guest
Posts: n/a
 
      01-04-2010
On Jan 4, 4:54*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik <(E-Mail Removed)> 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.









 
Reply With Quote
 
Shawn Milochik
Guest
Posts: n/a
 
      01-04-2010

On Jan 4, 2010, at 5:59 PM, Nav wrote:

> On Jan 4, 4:54 pm, Chris Rebert <(E-Mail Removed)> wrote:
>> On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik <(E-Mail Removed)> 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.
>
>
>
>
>


Then don't assign a variable to favorite, and just use name2drink["apple wine"].
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-04-2010
Nav wrote:
> On Jan 4, 4:54 pm, Chris Rebert <(E-Mail Removed)> wrote:
>> On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik <(E-Mail Removed)> 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/...amic-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/

 
Reply With Quote
 
Nav
Guest
Posts: n/a
 
      01-04-2010
Thanks Jan,
You read my mind. That is exactly what I needed.
Thanks for showing the product function from itertools as well. It
seems easier to grasp than the nested loops, I had been using.
I noticed chopin.edu.pl. Are you a musician?
Nav
 
Reply With Quote
 
Nav
Guest
Posts: n/a
 
      01-04-2010
Thanks for pointing it out Steve. The blog post doesn't explain it
very well. I understand the risk of exec or eval(input). but what are
the risks of globalnamespace use and what are the benefits?
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-05-2010
On Jan 5, 9:33*am, Nav <(E-Mail Removed)> wrote:
> what are the risks of globalnamespace use


You're unnecessarily tying your code to the implementation.

> and what are the benefits?


Absolutely none that using a dictionary doesn't also give you.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie - WEbrick, config/routes.rb and multiple instances Iain Barnett Ruby 4 06-27-2010 05:44 PM
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
restarting multiple instances of apache - newbie Q mikeisgreat@gmail.com Python 0 06-03-2006 06:05 PM
TB multiple instances - newbie question ciqret Firefox 2 06-02-2005 10:34 PM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM



Advertisments