Is there a way that I can define __init__
so keywords defined in **kwargs
are assigned to the class?
For example, if I were to initialize a ValidationRule
class with ValidationRule(other='email')
, the value for self.other
should be added to the class without having to explicitly name every possible kwarg.
class ValidationRule:
def __init__(self, **kwargs):
# code to assign **kwargs to .self
This may not be the cleanest way, but it works:
class ValidationRule:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
I think I prefer ony's solution because it restricts available properties to keep you out of trouble when your input comes from external sources.