Last active
August 29, 2015 13:57
-
-
Save jqlblue/9618218 to your computer and use it in GitHub Desktop.
python decorator with callable class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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