Day33.class机制分析

1.class机制分析_类的三大特征

'''class关键字创造类People的步骤'''
# todo 类有三大特征
# 1. 类名
class_name = 'People'
# 2. 类的基类
class_bases = (object, )
# 3. 执行类体代码,拿到类的名称空间
class_dic = {}
class_body = """
def __init__(self, name, age):
    self.name = name
    self.age = age

def say(self):
    print('{}:{}'.format(self.name, self.age))
"""
exec(class_body, {}, class_dic)

print('class_dic:{}'.format(class_dic))

# 4. class会调用元类
People = type(class_name, class_bases, class_dic)

obj = People('egon', 18)
print(obj.__dict__)
obj.say()

 

posted on 2024-07-12 16:54  与太阳肩并肩  阅读(2)  评论(0编辑  收藏  举报

导航