什么是元类

一切皆对象:类实际上就是一个对象

Person类也是一个对象,那他一定是由一个类实例化得到的,这个类就是元类
type是内置的一个元类,所有的类都是由type实例化得到的
产生类的类叫做元类

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

如何找到元类?

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

同理:type类是产生所有类的元类

print(type(dict))
print(type(list))
print(type(str))
print(type(object))
print(type(type))

猜你喜欢

转载自www.cnblogs.com/luodaoqi/p/11528870.html