dynamically extend classes

Robert W. Bill rbill at visi.com
Sat Feb 19 14:38:23 EST 2000


>Matt wrote:
>Is there a way to dynamically extend classes in Python?
>
>I'd like to be able to have sitecustomize.py import a module
>and add methods to a class so that later, when anohter modules
>imports this module and uses the class it sees the added methods.

Matt,
This is something I would like to do better myself- that is if you
mean run-time code generation and metaprogramming.  It has been 
often useful to have a program write a module and load it, but 
I have never wrote to a file that is currently a loaded module,
closed the file and did a reload.  It seems sensible though.  This
does bring up a question of mine though.  MetaClasses?

This type of situation is where MetaClasses are useful, according
to my meager understanding.  IBM SOMobjects made sense to me in
this type of situation, DTS C++ was somewhat in my grasp some 
time ago, but I have not had much luck with this in Python yet.  
Yes, I've read the 'Killer Joke' essay and related postings, but
I still find it extremely difficult to do in Python (I am
admittedly dense in the Python arena though).  Is
it possible that a MetaClass type (maybe I just mean keyword, or
syntax- I dunno) would help?  Has anyone made such a type? 
Has anyone pursued MetaClasses beyond "Eiffel.py" and found
them useful?  Is a MetaClass type needed to successfully use
mixins? (in multi-inderitance- I thought mixins were optionally-
even decided at run-time- added classes that impart a behavior 
or quality to a subclass. I apologize to you object gurus if I'm 
way off :).  I also crave metaclasses for object proxies, distributed
persistence, method dispatch, as well as the trace and pre/post tools
that Guido has provided.

Most people posting here are so far beyond me in understanding
objects that I realize I may be way off track.  So please feel
free to let me know how far off!  Which of you
object greats use metaclasses?  Is my understanding of them
way off?  In real-life designs, are they impractical?

Thank you in advance for any clarification.





More information about the Python-list mailing list