pythonbook/Python高级编程/使用type创建类.py

10 lines
280 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Foo = type('Foo', (), {'bar':True})
# class Foo(object):
# bar = True
def echo_bar(self): # 定义了一个普通的函数
print(self.bar)
FooChild = type('FooChild', (Foo,), {'echo_bar': echo_bar}) #让FooChild类中的echo_bar属性指向了上面定义的函数