Concrete classes -- stylistic question
Alex Martelli
aleax at aleax.it
Fri Oct 11 02:31:48 EDT 2002
More information about the Python-list mailing list
Fri Oct 11 02:31:48 EDT 2002
- Previous message (by thread): Concrete classes -- stylistic question
- Next message (by thread): Concrete classes -- stylistic question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Friday 11 October 2002 02:25, Ian Bicking wrote: > On Thu, 2002-10-10 at 16:29, Alex Martelli wrote: > > Gerhard Häring wrote: > > ... > > > > > Now who will come up with a neat example using metaclasses? > > > > I borrowed Guido's time machine to post about that: > > > > http://tinyurl.com/1wq1 > > I should really learn to understand what's going on with metaclasses, > but here's my attempt at similar functionality without metaclasses... Pretty good! A metaclass that only defines __new__, as metaMetaBunch does, is roughly equivalent to a factory function, such as yours. Even then, of course, using a metaclass lets you use normal class syntax rather than function-call syntax -- class XX(blah): etc rather than XX = blah(etc) -- but metaclasses do offer more. Say that you have metaMetaBunch in some library, and a new requirement comes up -- in some cases you want repr(X), where X is a class with that meta, to follow the usual convention, so that eval(repr(X)) == X. If type(X) is forced upon you, i.e. if you do NOT use a custom meta, you just can't -- you don't have any control on the results of repr(X) nor on the comparison of two such classes. With the custom metaclass, it becomes easy -- even if you need to leave the original metaclass alone, you can reuse by inheritance, as usual: subclass the meta and add __repr__ and __eq__ to it -- such methods defined in the metaclass control behavior of the metaclass's instances, i.e., the classes that instantiate it, just as for other cases of class / instance relationships. Alex to
- Previous message (by thread): Concrete classes -- stylistic question
- Next message (by thread): Concrete classes -- stylistic question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list