Skip to content

Instantly share code, notes, and snippets.

@socrateslee
Created May 28, 2014 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save socrateslee/ce1c126b56e500e043f0 to your computer and use it in GitHub Desktop.
Save socrateslee/ce1c126b56e500e043f0 to your computer and use it in GitHub Desktop.
A simple wrapper make a class become a functor.
'''
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