我试图确定我通过参数收到的类是否在传递的类中具有用户定义的__init__
函数。不在任何超级班。
class HasInit(object):
def __init__(self):
pass
class NoInit(object):
pass
class Base(object):
def __init__(self):
pass
class StillNoInit(Base):
pass
def has_user_defined_init_in(clazz):
return True if # the magic
assert has_user_defined_init_in(HasInit) == True
assert has_user_defined_init_in(NoInit) == False
assert has_user_defined_init_in(StillNoInit) == False
答案 0 :(得分:6)
我认为这会奏效:
def has_user_defined_init_in(clazz):
return "__init__" in clazz.__dict__