Twenty-one. Metaclass object-oriented type

A.  Metaclass (the metaclass that)

Everything in python object, we use the class keyword to define the class itself is an object, the object's class is responsible for generating called metaclass that may be referred to as meta-class class class 


https: //www.cnblogs. com / nickchen121 / p / 10992975.html detail .........

https://www.cnblogs.com/linhaifeng/articles/8029564.html detail .......


class Foo(object):
    ...
aa=Foo()
print(type(aa))  # <class '__main__.Foo'>
print(type(Foo))  # <class 'type'>

print("***********************************************************")

Per=type('Per',(object,),{"x":1})
print(Per.__dict__)
print(Per.x)


print("***********************************************************") 

# Type of class is instantiated class type metaclass 
DEF  __init__ (Self, name, Age): 
    self.name = name 
    self.age = Age
 DEF AA (Self):
     Print ( " This is the kind of class metaclass type to create the ha ha ha " )
     Print (the self.name) 

Per = type ( ' Per ' , (Object,), { ' X ' :. 1, ' the __init__ ' : the __init__ , ' AA ' : AA})
 Print (Per)
 Print (Per.__dict__ )
 # a1 = of Per ( "Joe Smith", 55) 
Print (Per.x)
 # 
# f1 = of Per ( "showcase", 55) 
# Print (f1.name)

 

Three elements to create a class: class name, base class, the class namespace 

People = of the type (class name, base class, the class namespace) 

class_name = ' People '   # class name 

class_bases = (Object,)   # base class 

# namespace class 
class_dic = {} 

class_body = "" " 
Country = 'China' 
DEF the __init __ (Self, name, Age): 
    the self.name name = 
    self.age = Age 
DEF EAT (Self): 
    Print ( '% S iS eating 'the self.name%) 
"" " 

Exec ( 
    class_body, 
    {}, 
    class_dic, 
)

 

Reproduced in: https: //www.cnblogs.com/Sup-to/p/11091146.html

Guess you like

Origin blog.csdn.net/weixin_33805557/article/details/94569045