Generating Multiple Class Instances
Julie Torborg
jtorborg at fnal.gov
Wed Jun 6 17:51:39 EDT 2001
On Wed, 6 Jun 2001, Paul Prescod wrote:
>
> There's something funny there. How does each Quark know what its color
> and mass is? What is the definition of getcolor and getmass?
where getcolor and getflavor are already defined, and you're right, I can
get this part working fine...
> > flavor=['up', 'down', 'strange', 'charm', 'top', 'bottom']
> > for each in flavor:
> > each=Quark()
>
> You aren't changing the list. You're just reading from it, never writing
> to it. Here's a straightforward way to do it:
I don't want to change the list, I want to do the same thing as typing
i_top=Quark(top)
i_bottom=Quark(bottom)
> flavornames = ['up', 'down', 'strange', 'charm', 'top', 'bottom']
> flavors = []
> for each in flavornames:
> flavors.append(Quark())
>
> for each in flavors:
> print each.color
This just creates a new ordered array. If I want to access the mass of
top, I still have to know where in the array the top instance lives.
What I want to do, is starting with my original list, is to make another
list (which is composed of a simple variation on the original list, like
adding the i_ above). Then if I want to know the mass of charm, I just
type
moc=i_charm.mass
and I don't have to know where anything lives.
Eventually, all of this will be going into a GUI, and access to all these
parameters will be very non-linear. The big idea here is to keep all the
"attributes" associated with their "objects." It seems to be so well
described by the OOP jargon I can't believe I'm having trouble. What I
visualize is this:
You've got a whole bunch of balloons (the class) filled with
different gases (the instance). Each has a different color (the "name"), a
string hanging from it and a sticker stuck to it (the attributes). On the
sticker and the string are written words. Now, I set all these balloons
bouncing around. How do I find what's written on the sticker of the
balloon with helium in it? Well, I have to know what color that balloon
is, then I can say, "Get the orange balloon." When the orange balloon
comes to me, I look at the sticker and say "Eureka." Now if all the
balloons are the same color, I have to go through all the balloons, or I
have to find another way of "naming" them, like putting them in order on a
line (the ordered list you suggest). The problem with that is that not
only do I have to know what gas is in what balloon, I have to keep them in
order or I lose that information.
But, to be fair, the solution you've provided here is better than what
I've come up with. Maybe there's a way to automatically build a
dictionary that will associate a "name" with each instance. That still
seems like a lot of keystrokes, not to mention CPU load during lookup
(like I said, there are hundreds of list items, I fictionalized this one
for simplicity), especially when I have a gut feeling that with a little
finesse, there's a much more elegant solution. Or there should be.
More information about the Python-list
mailing list