Created
July 26, 2018 14:34
-
-
Save vanlummelhuizen/50852af25b4bb7af08f3e5c09a72f32d to your computer and use it in GitHub Desktop.
Class decorator with method overriding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def my_class_decorator(cls): | |
| class NewClass(cls): | |
| def my_method(self, *args, **kwargs): | |
| print("I am the extended my_method.") | |
| super(NewClass, self).my_method(*args, **kwargs) | |
| print("I am the extended my_method, again.") | |
| return NewClass | |
| @my_class_decorator | |
| class MyClass: | |
| def my_method(*args, **kwargs): | |
| print("I am the original my_method.") | |
| if __name__ == '__main__': | |
| obj = MyClass() | |
| obj.my_method() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment