Skip to content

Instantly share code, notes, and snippets.

@mrjohannchang
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrjohannchang/45e4917058482005f8a9 to your computer and use it in GitHub Desktop.
Save mrjohannchang/45e4917058482005f8a9 to your computer and use it in GitHub Desktop.
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