Generating Multiple Class Instances

Joe Murray jmurray at agyinc.com
Wed Jun 6 17:06:49 EDT 2001


Julie Torborg wrote:
> 
> I'm no wiz at OOP, but I have the following problem:
> 
> I have a list of some hundreds of items, each of which has several
> attributes,What I want to do is peg each of the values to the list item
> and move it around my program.  This seems like the perfect opportunity
> to take advantage of the OOP aspect of Python by setting up a class, but
> I can't create or use instances without hard-wiring it for every list
> item.  What I want to do is:
> 
> class Quark:
>     def __init__(self):
>         self.color=getcolor()
>         self.mass=getmass()
> 
> flavor=['up', 'down', 'strange', 'charm', 'top', 'bottom']
> for each in flavor:
>     each=Quark()

This loop does not really do what you want it to do!

> ###Then later:
> for each in flavor:
>     print each.color
> 
> ###What I'm relegated to doing is:
> 
> up=Quark()
> down=Quark()
> strange=Quark()...
> 
> print up.color
> print down.color
> print strange.color...
> 
> Which works, but with hundreds of list items, gets messy in a hurry.
> I've also tried messing around with using something that looks more like
> quark=Quark() several times, but then all the info stored in the
> previous instance gets obliterated.  I've also tried using lists and
> dictionaries, but they're bulky and what's the point of using an OOP
> language if there's no additional functionality or elegance?
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

I think you were on the right track, but you relegated yourself to doing
something you need not do! :)

This should work:

-----CODE-----

def getColor():
    return raw_input('Enter a color: ')

def getMass():
    return raw_input('Enter a mass: ')

class Quark:
    def __init__(self, color, mass):
        self.color = color
        self.mass = mass

flavor = ['up', 'down', 'strange', 'charm', 'top', 'bottom']

# create a list of quarks, based on the elements of flavor, I suppose
quarks = []
for each in flavor:
    quarks.append(Quark(getColor(), getMass()))

# print the colors of all the quarks
for quark in quarks:
    print quark.color


-----END OF CODE-----

Note, this simply creates an instance of the Quark class for every
element in flavor.  I don't know if this is what you want to do, but I
hope this helps.  Note that Quark instantiations are stored in their own
list.  Note that the placeholder 'each' can NOT be used to do an inplace
replacement of the elements of flavor.

Lists are generally not bulky; dictionaries can get bulky.  They are
standard python datatypes which are generally very helpful.  Lists and
dictionary objects provide functionality and elegance in many
situations.  Note that 'flavor' is a list and so is 'quarks'. 
quarks.append(...) is a method of the python list object.  That's pretty
functional and elegant, right ;) ?

Good luck, feel free to ask more questions, the python-list is a helpful
bunch...

joe

-- 
Joseph Murray
Bioinformatics Specialist, AGY Therapeutics
290 Utah Avenue, South San Francisco, CA 94080
(650) 228-1146




More information about the Python-list mailing list