Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # a lot of code ommitted
- class FilterAction(object):
- def __init__(self, **kwargs):
- # setting default attributes for base class (lets say this class is base)
- self._set_default(kwargs, 'important_attribute', "Important attribute")
- # this method will be defined in BaseAction so it will be in one place
- # it will be used for setting all default values, so it is consistent in all classes
- def _set_default(self, kwargs, key, default):
- value = (kwargs.get(key) if kwargs.get(key) is not None
- else default)
- setattr(self, key, value)
- class MyGreatFilterAction(FilterAction):
- def __init__(self, **kwargs):
- super(MyGreatFilterAction, self).__init__(**kwargs)
- # redefining default attributes values for this class
- self._set_default(kwargs, 'important_attribute', "I am not so important in this class")
- def print_important_attribute(self):
- print self.important_attribute
- m = MyGreatFilterAction(important_attributes=None)
- m.print_important_attribute()
- # the 'bla' attribute is just thrown away, beacause
- # no level of inheritance tree assigns it as attribute of the object
- # It could also throw exception when unknown attribute is given
- m = MyGreatFilterAction(important_attribute="I can be important for this object", bla="blah")
- m.print_important_attribute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement