Lists of attributes

Bruce Eckel Bruce at EckelObjects.com
Thu Dec 6 21:07:44 EST 2001


>Anyway, why can't you just stick each instance in a global
variable
>(preferably a dictionary or dictionary-like object)? If you really
like the
>attribute assignment syntax, you can make a class that allows
element
>reference both through attributes (yuck) and through item indexing
.

Right, missed that. Now it's even cleaner:
import random
rgen = random.Random()
flwrs = [Gladiolus(),Runuculus(),Chrysanthemum()]
flowers = [rgen.choice(flwrs) for i in range(10)]

This produces a random selection of references to only three
objects.

I'd still like to not have to explicity enumerate flwrs. I may play
with Tim's suggestion:
    class Flower(object): 
	...
and
    Flower.__subclasses__()

Except that I haven't been able to locate any actual information on
how to get '__subclasses__()' to work.


Most current information can be found at:
http://www.mindview.net/Etc/notes.html
===================
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java 2e" & "Thinking
in C++ 2e"
Please subscribe to my free newsletter -- just send any email to:
join-eckel-oo-programming at earth.lyris.net
My schedule can be found at:
http://www.mindview.net/Calendar
===================






More information about the Python-list mailing list