Browsing articles tagged with " Tricks"
Jun
24

# Override decorator

By Eric Shull  //  Blog Post  //  No Comments

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!
Feb
25

# Python Dictionary Coolness

Lately I’ve been working with dictionaries, specifically as a device for storing settings. I ran into a little annoyance and thought of programming a little routine to take care of it. Come to find out, Python already has such a routine built into the dictionary class. Read on to find out more.

read more