第三十九节 元类创建,类创建实例对象

def xxx():
    pass

@classmethod
def yyy():
    pass

Test = type('Test', (), {'num1':1,'num2':2})

# 继承test,并且定义了两个方法
Test2 = type('Test2', (Test,), {'xxx':xxx,'yyy':yyy})

print(30*'#')
help(Test)
help(Test2)
print(30*'#')

print(type)  # 实质上,type是一个类,不是函数
print(Test)

class Test1():
    pass
'''
用type动态的创建类,
第一参数:类名
第二参数:元组,传入父类名
第三参数:类属性
实际上调用class创建类时,本质上就是在调用type,即type就是元类
'''
# 打印两个类的父类,都是type
print(Test.__class__)
print(Test1.__class__)

猜你喜欢

转载自www.cnblogs.com/kogmaw/p/12602593.html
今日推荐