一、创建类的执行流程

 

二、元类的认识

什么是元类呢?在Python3中继承type的就是元类

二、元类的示例

 方式一:

# 方式一
class MyType(type):
    '''继承type的就是元类'''
    def __init__(self,*args,**kwargs):
        print("MyType创建的对象",self)   #Foo
        super(MyType,self).__init__(*args,**kwargs)

    def __call__(self, *args, **kwargs):
        obj = super(MyType,self).__call__(*args,**kwargs)
        print("类创建对象",self,obj)   #Foo

class Foo(object,metaclass=MyType): #  对象加括号会去执行__call__方法,__call__方法里面继承了type的__call__方法
                                     ,type的__call__方法里面会先执行__new__方法,再去执行__init__方法。
                                      所以,Foo就是用type创建出来的
    user = "haiyan"
    age = 18

obj = Foo()

 

方式二

# 方式二
class MyType(type):
    def __init__(self, *args, **kwargs):
        print("ssss")
        super(MyType, self).__init__(*args, **kwargs)

    def __call__(cls, *args, **kwargs):
        v = dir(cls)
        obj = super(MyType, cls).__call__(*args, **kwargs)
        return obj
#对象加括号就会去执行__call__方法
class Foo(MyType('Zcc', (object,), {})):  #MyType('Zcc', (object,), {})相当于class Zcc(object):pass,也就是创建了一个Zcc的类
    user = 'haiyan'
    age = 18

obj = Foo()

 

方式三

# 方式三
class MyType(type):
    def __init__(self, *args, **kwargs):
        print("ssss")
        super(MyType, self).__init__(*args, **kwargs)

    def __call__(cls, *args, **kwargs):
        v = dir(cls)
        obj = super(MyType, cls).__call__(*args, **kwargs)
        return obj
#对象加括号就会去执行__call__方法

def with_metaclass(arg,base):
    print("类对象",MyType('Zcc', (base,), {}))
    return arg('Zcc', (base,), {})  #返回一个类对象  <class '__main__.Zcc'>

class Foo(with_metaclass(MyType,object)):  #MyType('Zcc', (object,), {})相当于class Zcc(object):pass,也就是创建了一个Zcc的类
    user = 'haiyan'
    age = 18

obj = Foo()

 

=======================附加===========================

 1 class ASD(type):
 2     pass
 3 
 4 qqq = ASD("qwe", (object,), {})  #用ASD这个元类创建了一个(qwe,并且继承object类的)类
 5 
 6 # class ASD(qwe):
 7 #     pass
 8 obj = qqq()
 9 # 能创建类的是元类
10 # 能创建对象的是类
11 print(obj)  #<__main__.qwe object at 0x00000000024FFBA8>
12 print(obj.__class__)  #<class '__main__.qwe'>
13 print(obj.__class__.__class__)  #<class '__main__.ASD'>
14 print(obj.__class__.__class__.__class__)  #<class 'type'>
15 print(obj.__class__.__class__.__class__.__class__)  #<class 'type'>
View Code

 

posted on 2018-01-10 20:31  海燕。  阅读(876)  评论(0编辑  收藏  举报