用type方法创建类

def __init__(self,name,age):
self.name=name
self.age=age

def hello(self):
print ('%s is saying hello...' %self.name)

Foo=type('Foo',(object,),{'hello':hello,'__init__':__init__})

f=Foo('zhang',22)
print (f.name)
print (f.age)
f.hello()
posted @ 2018-05-13 19:58  Python从入门到放弃第一集  阅读(250)  评论(0编辑  收藏  举报