什么是元类

网站:https://www.cnblogs.com/linhaifeng/articles/8029564.html

一等公民:只要把一个东西赋值给一个变量,这个东西就叫一等公民

什么是元类:

Person类也是一个对象,那他一定是由一个类实例化得到,这个类,就叫元类

type是内置的一个元类,所有的类都是有type实例化得到

产生类的类就是元类:在python中,类是对象的模板,而元类则是类的模板。元类用于创建类,就像类用于创建对象一样。

class Person:
    def __init__(self, name):
        self.name = name
    def score(self):
        print("分数为100")
      
p = Person('nick')
print(p.name)

如何找元类?

# 查看对象(p)的类型
print(type(p))		# 结果为: < class 'Person' >
# 同理,查看Person类的类型;	type也就是元类, type类是产生所有类的元类
print(type(Person))		# 结果为: 	< class 'type' >
print(type(dict))
print(type(list))
print(type(str))
print(type(object))		# 等等结果都是: < class 'type' >
posted @ 2023-10-30 11:22  hanyingshuo  阅读(13)  评论(0)    收藏  举报