Skip to content

Instantly share code, notes, and snippets.

@guidoism
Created August 16, 2011 18:12
Show Gist options
  • Save guidoism/1149757 to your computer and use it in GitHub Desktop.
Save guidoism/1149757 to your computer and use it in GitHub Desktop.
import functools
class a(object):
def __init__(self, f):
self.f = f
def __call__(self, *args):
obj = args and args[0]
print 'Decorator "a" on %s %s object' % (self.f.__name__, 'with' if obj else 'without')
def __get__(self, obj, objtype):
'Support instance methods'
print "Doing some important work in a's get"
return functools.partial(self.__call__, obj)
@a
def foo():
pass
foo()
class Bar(object):
@a
def bar(self):
pass
Bar().bar()
class b(object):
def __init__(self, my_arg):
self.my_arg = my_arg
def __call__(self, f):
def wrapped(*args):
obj = args and args[0]
print 'Decorator "b" on %s %s object' % (f.__name__, 'with' if obj else 'without')
return wrapped
def __get__(self, obj, objtype):
'Support instance methods'
print "Doing some important work in b's get"
return functools.partial(self.__call__, obj)
@b('my cool arg')
def baz():
pass
baz()
class Goo(object):
@b('my cool arg')
def goo(self):
pass
Goo().goo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment