Inside the Head of PyDanny

Hi, I'm Daniel Roy Greenfeld, and welcome to my blog. I write about Python, Django, and much more.

I wanted to see if I could have an exception as a decorator argument. Here is what I came up with:

import functools

class MyGoof(Exception):

def pass_goof(exception):
    def decorator(test_func):
        def wrapper(*args, **kwargs):
                return test_func(*args, **kwargs)
            except exception as e:
                return None
        return wrapper
    return decorator

def test1():
    return "test1"

def test2():
    raise MyGoof

assert test1() == 'test1'
assert test2() == None

Published: 2013-12-24 13:15

Tags: python howto


If you read this far, you might want to follow me on twitter or github and subscribe via email below (I'll email you new articles when I publish them).



Content Copyright © 2012-2018 Daniel Greenfeld. Proudly harnessed by Mountain, powered by Flask, and rendered by Frozen Flask, all of which take great advantage of Python.