BillBie

导航

python动态生成类

动态生成类的可使用type()

一.先查看普通创建类的方法

    class A():
        def __init__(self):
            print('A初始化')

    class B(A):
        def __init__(self):
            super().__init__()

    a=A()
    print(type(A))
    b=B()

执行的结果为:

 

 

二.使用type 创建类

    C=type('C',(object,),class_dict)
    print(type(C))
    c=C()
    print(c.x)

执行结果为:

三.使用type 创建类并继承

posted on 2019-04-24 18:18  BillBie  阅读(22)  评论(0)    收藏  举报