[Python-3000] Metaclass Vs Class Decorator

Nick Coghlan ncoghlan at gmail.com
Mon May 19 07:06:55 CEST 2008


paul bedaride wrote:
> I also wonder if we need this two things, and if that is not two way to 
> explain
> the same semantic.

Changing the metaclass can lead to some fundamental changes to the way a 
class operates. Class decorators are for simpler things which don't 
require major changes to the class, and, in particular, things which 
shouldn't automatically be inherited by subclasses.

The specific motivating example in the python-dev thread linked from PEP 
3129 was a class registry where being a subclass of an already 
registered class didn't necessary imply that the subclass should also be 
registered. This semantic is painful to implement using a metaclass, but 
trivial with a class decorator.

"Should subclasses implicitly inherit this behaviour" is actually a 
pretty decent rule of thumb for deciding whether something should be 
handled with a metaclass or a class decorator.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list