Lists of attributes

Terry Reedy tjreedy at home.com
Thu Dec 6 15:36:08 EST 2001


"Bruce Eckel" <Bruce at EckelObjects.com> wrote in message
news:mailman.1007668580.29932.python-list at python.org...
> If I have:
>
> class Flower:
>   def accept(self, visitor):
>     visitor.visit(self)
>   def __repr__(self):
>     return self.__class__.__name__
>
> class Gladiolus(Flower): pass
> Flower.gladiolus = Gladiolus()
>
> class Runuculus(Flower): pass
> Flower.runuculus = Runuculus()
>
> class Chrysanthemum(Flower): pass
> Flower.chrysanthemum = Chrysanthemum()
>
> Is there a clever way to get a list containing
> [Flower.gladiolus, Flower.runuculus, Flower.chrysanthemum]
>
> I've got:
> [j for i,j in Flower.__dict__.items() if Flower in
> j.__class__.__bases__]
> But I was hoping for something less awkward...

That strikes me a pretty clever.  Alternatives are to encode
'subclassness' in names or keep explicit list:
class Flower:
  subclasses = []
...
Flower.subclasses.append[Gladiolus] #or whatever you want

Terry J. Reedy







More information about the Python-list mailing list