Dynamically created method and decorator, got error 'functools.partial' object has no attribute '__module__'

前端 未结 7 775
面向向阳花
面向向阳花 2020-12-31 01:58

I am currently using EndpointsModel to create a RESTful API for all my models on AppEngine. Since it is RESTful, these api have a lot of repeat code which I want to avoid.

7条回答
  •  长情又很酷
    2020-12-31 02:44

    In our case I solved this by subclassing functools.partial:

    class WrappablePartial(functools.partial):
    
        @property
        def __module__(self):
            return self.func.__module__
    
        @property
        def __name__(self):
            return "functools.partial({}, *{}, **{})".format(
                self.func.__name__,
                self.args,
                self.keywords
            )
    
        @property
        def __doc__(self):
            return self.func.__doc__
    

    NB you could also make use of __getattr__ to redirect queries, but I figured that was actually less readable (and makes it more difficult to insert any useful meta-data as with __name__)

提交回复
热议问题