Newbie Class Questions
Joe Mason
joe at notcharles.ca
Sat Mar 27 22:45:32 EST 2004
In article <94CdnVqi7o8rn_vdRVn-vg at centurytel.net>, Ed Suominen 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?
Check out http://www.phyast.pitt.edu/~micheles/python/meta1.html and
http://www.phyast.pitt.edu/~micheles/python/meta2.html. It's advanced,
but it'll do what you want.
I just used your question as an example in the "Prothon Prototypes vs
Python Classes" thread, including the actual metaclass that'll do what
you want. Here it is again:
autoall = []
def AutoAll():
l = []
for i in autoall:
l.append(i())
return l
class AutoRegister(type):
def __init__(klass, name, bases, dict):
autoall.append(klass)
Then just add the line "__metaclass__ = AutoRegister" to your AutoText
definition, and all its subclasses will automatically inherit it.
> I have all of 3 days experience with OOP and Python, so please bear with me.
Might be a little early for metaclasses, then...
Joe
More information about the Python-list
mailing list