Last active
January 2, 2016 01:41
-
-
Save Xpktro/00e0a39c605cdf1fd0b7 to your computer and use it in GitHub Desktop.
Simple automatic wrappers in Python
This file contains 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
class Window(object): | |
def _draw(self): | |
print 'parent draw' | |
def __getattribute__(self, name): | |
if hasattr(self, '_' + name): | |
original = object.__getattribute__(self, '_' + name) | |
new = object.__getattribute__(self, name) | |
def wrapper(*args, **kwargs): | |
original(*args, **kwargs) | |
new(*args, **kwargs) | |
return wrapper | |
else: | |
object.__getattribute__(self, name) | |
class MyWindow(Window): | |
def draw(self): | |
print 'my draw' | |
w = MyWindow() | |
w.draw() | |
# Output: | |
# parent draw | |
# my draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment