Skip to content

Instantly share code, notes, and snippets.

@Xpktro
Last active January 2, 2016 01:41
Show Gist options
  • Save Xpktro/00e0a39c605cdf1fd0b7 to your computer and use it in GitHub Desktop.
Save Xpktro/00e0a39c605cdf1fd0b7 to your computer and use it in GitHub Desktop.
Simple automatic wrappers in Python
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