# -*- coding: utf-8 -*-
# @Time : 2021/8/1 18:48
# @Author : zy7y
# @Gitee : https://gitee.com/zy7y
# @File : metaclass.py
# @Project : PythonBooks
"""
类也是对象,type是创建类的类, type 也是object 的子类
"""
# 动态创建类 type
"""
type(object_or_name, bases, dict)
type(object) -> the object's type
type(name, bases, dict) -> a new type
# (copied from class doc)
"""
def say(self):
"""该方法 需要给实例,绑定到元类上"""
print("say.")
return 1
# 创建 User类, () 不继承类, {} 属性 或者方法
User = type("User", (), {"name": "小明", "say": say})
class Super(type):
def __new__(cls, *args, **kwargs):
return super().__new__(cls, *args, **kwargs)
class User(metaclass=Super):
"""实例化过程中 寻找metaclass ,通过它去创建User类, 找不到时 就会调用type 创建类"""
def __str__(self):
return "User obj"
if __name__ == '__main__':
user = User()
# print(user.name)
# print(user.say())
print(user)