Skip to content

Instantly share code, notes, and snippets.

@jqlblue
Last active August 29, 2015 13:57
Show Gist options
  • Save jqlblue/9618218 to your computer and use it in GitHub Desktop.
Save jqlblue/9618218 to your computer and use it in GitHub Desktop.
python decorator with callable class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# example of a decorator from http://xahlee.info/perl-python/python_decorator.html
class Foo:
def __init__(self, method):
self.__method = method
def __call__(self, arg):
return self.__method(arg - 1)
def gg(call_fun):
print "gg called"
foo_instance = Foo(call_fun)
return foo_instance
@gg # ← this is decorator
def ff(x):
print "ff called"
return x+1
print ff(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment