Created
May 28, 2014 06:23
-
-
Save socrateslee/ce1c126b56e500e043f0 to your computer and use it in GitHub Desktop.
A simple wrapper make a class become a functor.
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
''' | |
A simple wrapper make a class become a functor, | |
the class need a __call__ function. The (*sub, | |
**kwargs) will be passed for the __init__ function, | |
and the name of class will be the name of function | |
object. | |
Example | |
------- | |
@functor(*sub, **kwargs) | |
class functor_name(object): | |
def __init__(self, *sub, **kwargs): | |
... | |
def __call__(self): | |
... | |
Then you may use functor_name as a function. | |
''' | |
import sys | |
def functor(*sub, **kwargs): | |
def wrapper(cls): | |
module = sys.modules[cls.__module__] | |
obj = cls(*sub, **kwargs) | |
setattr(module, cls.__name__, obj) | |
return obj | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment