Skip to content

Instantly share code, notes, and snippets.

@changyuheng
Last active Aug 29, 2015
Embed
What would you like to do?
Python preprocessed decorator
class Api:
def __init__(self):
self.apis = []
def register(self):
s = self
class Api:
def __init__(self, func):
self.func = func
s.apis.append(func.__name__)
def __call__(self, *args, **kwargs):
self.func(*args, **kwargs)
return Api
import api
myapi = api.Api()
@myapi.register()
def p(s, q=None):
print(s)
print(q)
@myapi.register()
def b():
print('b')
myapi2 = api.Api()
@myapi2.register()
def c():
print('c')
print(myapi.apis)
print(myapi2.apis)
p('s', q='q')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment