Generating Multiple Class Instances

Alex Martelli aleaxit at yahoo.com
Wed Jun 6 17:50:44 EDT 2001


"Paul Prescod" <paulp at ActiveState.com> wrote in message
news:mailman.991861150.19995.python-list at python.org...
    ...
> You aren't changing the list. You're just reading from it, never writing
> to it. Here's a straightforward way to do it:
>
> flavornames = ['up', 'down', 'strange', 'charm', 'top', 'bottom']
> flavors = []
> for each in flavornames:
> flavors.append(Quark())

More straightforward IMHO:
    flavors = [Quark() for each in flavornames]

> for each in flavors:
>     print each.color
>
> I still don't know where to get the colors or masses. Also, don't you
> want to somehow associate the flavornames with the quarks you create?

I hypothesized in another post (where I shared just your doubts,
but we 'crossed':-) to serve both purposes by giving Quark's
__init__ the flavorname as argument:
    flavors = [Quark(each) for each in flavornames]
presumably the body of Quark.__init__ would bind that name
as self.name as well as pass it to getcolor & getmass...


Alex







More information about the Python-list mailing list