高级使用
反射: getattr(obj,'xxx') 导入模块: import re 'django.middleware.clickjacking.XFrameOptionsMiddleware' m = importlib.import_module('django.middleware.clickjacking') cls = getattr(m,'XFrameOptionsMiddleware') cls() 面向对象: #示例一: 之后爬虫使用 class Foo: def __init__(self): pass @classmethod def instance(cls): return cls() def process(self): pass if hasattr(Foo,'instance'): obj = Foo.instance() else: obj = Foo() obj.process() #示例二: class A: def f1(self): self.f2() def f2(self): print('A.f2') class B(A): def f2(self): print("B.f2") obj = B() obj.f1()