:\codepy - extend-meta py ni!nini!ni!ni!nibaconham ni?ni?ni?nibaconham notice that the metaclass in this example still performs fairly static taskadding two known methods to every class that declares it in factif all we need to do is always add the same two methods to set of classeswe might as well code them in normal superclass and inherit in subclasses in practicethoughthe metaclass structure supports much more dynamic behavior for instancethe subject class might also be configured based upon arbitrary logic at runtimecan also configure class based on runtime tests class metaextend(type)def __new__(metaclassnamesupersclassdict)if sometest()classdict['eggs'eggsfunc elseclassdict['eggs'eggsfunc if someothertest()classdict['ham'hamfunc elseclassdict['ham'lambda *args'not supportedreturn type __new__(metaclassnamesupersclassdictmetaclasses versus class decoratorsround keep in mind again that the prior class decorators often overlap with this metaclasses in terms of functionality this derives from the fact thatclass decorators rebind class names to the result of function at the end of class statementafter the new class has been created metaclasses work by routing class object creation through an object at the end of class statementin order to create the new class although these are slightly different modelsin practice they can often achieve the same goalsalbeit in different ways as you've now seenclass decorators correspond directly to metaclass __init__ methods called to initialize newly created classes decorators have no direct analog to the metaclass __new__ (called to make classes in the first placeor to metaclass methods (used to process instance classes)but many or most use cases for these tools do not require these extra steps because of thisboth tools in principle can be used to manage both instances of class and the class itself in practicethoughmetaclasses incur extra steps to manage instancesand decorators incur extra steps to create new classes hencewhile their roles metaclasses