Using metaclasses to play with decorators.

David MacQuigg dmq at gain.com
Wed Jun 16 07:18:53 EDT 2004


On 15 Jun 2004 21:46:22 -0700, michele.simionato at poste.it (Michele
Simionato) wrote:

>IMO, you really need metaclasses only if you are building a framework such
>as Zope or Twisted, or you are a core Python developer. Mere mortals can have 
>only few reasonable usages for metaclasses: to play with them (which is good as 
>far as you don't put your experiment in production code), to use them for
>debugging, and to use them as a quick hack to fix code written by others
>or such that you want to change the source code as little as possible.

I decided to include a brief discussion of metaclasses in my chapter
introducing Python OOP for engineering students, but if there is a
simpler way to do what I need, I could eliminate this section and not
be tempting students to use (and possibly abuse) metaclasses. (See
example on p.13 in PythonOOP.doc at
http://ece.arizona.edu/~edatools/Python )

I haven't given this much thought, but it occurred to me that making
the __new__ function work in an ordinary class ( not just a metaclass
) would avoid the need for metaclasses in simple cases like my
example.  If the __new__ function is present in a class, then run it
when any new class is constructed with the current class as an
ancestor.  In my example, all I am doing is adding a class variable
'_count' to each new class.

By providing this simple functionality *without* metaclasses, we can
keep a clear line between what mere mortals need to learn, and what
the Python developers need.

-- Dave

*************************************************************     *
* David MacQuigg, PhD              * email:  dmq at gain.com      *  *
* IC Design Engineer               * phone:  USA 520-721-4583  *  *  *
* Analog Design Methodologies                                  *  *  *
*                                  * 9320 East Mikelyn Lane     * * *
* VRS Consulting, P.C.             * Tucson, Arizona 85710        *
*************************************************************     *




More information about the Python-list mailing list