Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determinate if class has user defined __init__

I'm trying to identify if a class that i received via an argument has a user defined __init__ function in the class that was passed. Not in any super class.

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
like image 649
RedX Avatar asked May 25 '16 05:05

RedX


1 Answers

I think this will work:

def has_user_defined_init_in(clazz):
    return "__init__" in clazz.__dict__
like image 62
Tom Karzes Avatar answered Sep 21 '22 17:09

Tom Karzes