Determine if user __init__ is defined

I am trying to determine if a class that I received through an argument has a user-defined function __init__ in the class that was passed. Not in the superclass.

 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 
+5
source share
1 answer

I think this will work:

 def has_user_defined_init_in(clazz): return "__init__" in clazz.__dict__ 
+6
source

All Articles