用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()

猜你喜欢

转载自www.cnblogs.com/zhangtianxia/p/9033110.html