python中创建类的方法:
方式一:利用class关键字
class Chinese(object): country='China' def __init__(self,name,age): self.name=name self.age=age def talk(self): print('%s is talking' %self.name)
方式二(就是手动模拟class创建类的过程):将创建类的步骤拆分开,手动去创建
def walk(self): print('=====>', self.name) class_name = 'Foo' bases = (object,) class_dict = { 'x': 1, 'run': walk } Foo = type(class_name, bases, class_dict) print(Foo.__dict__)
更多Python知识请关注