python中可以用class声明类,也可以通过以下方式声明类,其效果和类声明是一样的
#!/user/bin/env python # -*- coding:utf-8 -*- # python3.5 # @Time : 2017/10/23 21:26 # @Author : Shone # @File : test.py def fuc(self): print("hello") Foo = type('Foo', (),{'fuc':fuc}) a = Foo() a.fuc() print(type(Foo))
结果:
hello <class 'type'>
以上代码和用class声明的以下代码效果是一致的
class Foo: def fuc(self): print("hello") a = Foo() a.fuc() print(type(Foo))结果:
hello <class 'type'>