Last active
August 29, 2015 14:21
-
-
Save mrjohannchang/45e4917058482005f8a9 to your computer and use it in GitHub Desktop.
Python preprocessed decorator
This file contains hidden or 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
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 |
This file contains hidden or 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
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