VikkaLorel

class wrapper (decorator)

Feb 24th, 2020
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. def some_class_decorator(class_: object):
  2.     class ClassWrapper(class_):
  3.         new_attr = 'foo'
  4.  
  5.         def __init__(self):
  6.             super().__init__()
  7.             self.another_new_attr = 'bar'
  8.     return ClassWrapper
  9.  
  10.  
  11. @some_class_decorator
  12. class CustomType:
  13.     some_attr = 1
  14.  
  15.     def __init__(self):
  16.         self.another_attr = 2
  17.  
  18.  
  19. if __name__ == '__main__':
  20.     some_var = CustomType()
  21.     print(some_var.some_attr, some_var.another_attr,
  22.           some_var.new_attr, some_var.another_new_attr, sep='\n')
Advertisement
Add Comment
Please, Sign In to add comment