24
# Override decorator
While working with pyglet one day, I made the mistake of subclassing the window class and overriding the __init__ method, which prevented the window from showing. To fix this, it’s easy enough to add a call to super(Window, self).__init__(*args, **kwargs), but I went a different route. Here’s a decorator called override that puts it in for you:
def override(function):
def wrapper(instance, *args, **kwargs):
super(instance.__class__, instance).__init__(*args, **kwargs)
return function(instance, *args, **kwargs)
return wrapper
To use it, simply use the decorator notation in a subclassed method:
class MyWindow(pyglet.window.Window):
@override
def __init__(self, *args, **kwargs):
# code goes here, no need to call super!
10
# Function/Method Decorators
One of the reasons I like Python is because it’s simple but powerful. All the tools are there for whatever task needs to be done. I’ve found __getattr__ to be a very useful tool for creating sets of standardized class methods without having to explicitly declare them. Another useful tool is function decorators. Read on to get a quick idea of how to make your own class decorators.

