[Python-Help] how do I use the __metaclass__ variable to change the default base class?
Alex Martelli
aleaxit at yahoo.com
Mon Dec 2 04:12:22 EST 2002
More information about the Python-list mailing list
Mon Dec 2 04:12:22 EST 2002
- Previous message (by thread): how do I use the __metaclass__ variable to change the default base class?
- Next message (by thread): how do I use the __metaclass__ variable to change the default base class?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--- Adam Feuer <adamf at pobox.com> wrote: > Python folks, > > I have a piece of Python software made up of many > classes that I would > like to collect information about at runtime- > specifically, what > classes hold references to what other classes. > > I thought it would be possible to define my own > class that subclasses > object, and redefine the __setattr__ method on that > class. Then, when > I wanted to collect the information I would use the > __metaclass__ > global variable to tell Python at runtime that all > my classes now > derive from my new base class (that is, the default > base class for all > classes is now MyObject)... > > I haven't been able to get this to work. What's > wrong? You're confusing metaclasses and superclasses, which are two totally different concepts. When X is a class object, X's metaclass is type(X). That has nothing to do with X's base classes, which are X.__bases__. To build a custom metaclass, you normally subclass the built-in type and override some special methods. For example, to inject a base class B in every class whose metaclass is M, M.__new__ can suitably inject B among the bases if needed (the tuple of bases is passed as one of the arguments to M's special methods __new__ and __init__). Alex __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com
- Previous message (by thread): how do I use the __metaclass__ variable to change the default base class?
- Next message (by thread): how do I use the __metaclass__ variable to change the default base class?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list