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 创建类并继承

猜你喜欢

转载自blog.csdn.net/qq_34309753/article/details/89498900
今日推荐