A class does not declare its own metaclass metaclass is his default type, in addition to using the built-in metaclass type, we can also customize the inherited type metaclass, then use the keyword argument metaclass metaclass is specified as a class class Foo: DEF the __init__ (Self): pass F1 = Foo () # F1 by Foo instantiated objects # Print (type (F1)) Print (type (Foo)) Print (Foo. the __dict__ ) DEF the __init__ (Self, name, Age ): the self.name = name self.age = Age Ffo = type ( " Ffo " , (Object,), { ' X ' :. 1, " the __init__ ": __Init__ }) print (Fugitive) print (Fugitive. __Dict__ ) f1 = Fugitive ( " Alex " , 20 ) print (f1. __Dict__ ) print (f1.name)
#自定制元类
MyType class (type):
DEF the __init __ (Self, A, B, C):
Print ( "function execution metaclass")
Print (Self)
# Print (A)
# Print (B)
# Print (C)
#__call__ brackets behind the object, trigger the execution. Note: the constructor is executed by the object triggers created, namely: class name = Object (); the method is performed for __call__ bracketed by the object triggers, namely: Object () or class () ()
__ the __call DEF (Self, args *, ** kwargs):
Print ( "=======>")
Print (args, kwargs)
obj = new new Object .__ __ (Self) # -----> F1 generation target
.__ the init __ Self (obj, args *)
return obj
class Foo (= the metaclass that MyType): # = Foo MyType (parameters. 4) -> __ init__ ---> MyType (Foo, 'Foo', (object,), { })
DEF the __init __ (Self, name):
the self.name name =
Print (Foo)
Call method f1 = Foo ( "alex") # Foo () execution object
Print (f1.name)
# Print (f1.name)
# print (f1 .__ dict__)