Newbie Class Questions

Peter Otten __peter__ at web.de
Sun Mar 28 07:54:49 EST 2004


Joe Mason wrote:

>>> I think it would be cool for my "AutoText" class (see below, hereby
>>> GPL'd) to be able to tell what subclasses inherit from it and
>>> instantiate objects for all of those subclasses to implement a "AutoAll"
>>> function.. Any ideas?
> 
> Actually, somebody pointed out in the other thread that Python has a
> __subclasses__ function, so AutoAll can just do "for i in
> AutoText.__subclasses__:".

My first reaction was disbelief :-) Now we can change

# add as base whenever you create a new subclass of AutoText
class AutoAll(AutoUrl, AutoEmphasis): 
    pass

to

AutoAll = type("AutoAll", tuple(AutoText.__subclasses__()), {})

(assuming the subclasses are not subclassed themselves).

Do you know what the last dictionary parameter of type() is for? A cursory
glance over the 2.3 library did not bring up a usecase of type() with three
parameters.

Peter




More information about the Python-list mailing list