Advertisement
Guest User

horizon defaulst new

a guest
May 22nd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. # a lot of code ommitted
  2. class FilterAction(object):        
  3.     def __init__(self, **kwargs):    
  4.         # setting default attributes for base class (lets say this class is base)            
  5.         self._set_default(kwargs, 'important_attribute', "Important attribute")
  6.    
  7.     # this method will be defined in BaseAction so it will be in one place
  8.     # it will be used for setting all default values, so it is consistent in all classes
  9.     def _set_default(self, kwargs, key, default):
  10.         value = (kwargs.get(key) if kwargs.get(key) is not None
  11.                                  else default)
  12.         setattr(self, key, value)
  13.  
  14. class MyGreatFilterAction(FilterAction):  
  15.     def __init__(self, **kwargs):                
  16.         super(MyGreatFilterAction, self).__init__(**kwargs)
  17.         # redefining default attributes values for this class
  18.         self._set_default(kwargs, 'important_attribute', "I am not so important in this class")        
  19.        
  20.    
  21.     def print_important_attribute(self):
  22.         print self.important_attribute
  23.    
  24. m = MyGreatFilterAction(important_attributes=None)
  25. m.print_important_attribute()    
  26.  
  27. # the 'bla' attribute is just thrown away, beacause
  28. # no level of inheritance tree assigns it as attribute of the object
  29. # It could also throw exception when unknown attribute is given
  30. m = MyGreatFilterAction(important_attribute="I can be important for this object", bla="blah")
  31. m.print_important_attribute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement