On Fri, Feb 10, 2017 at 12:29 PM, Stephan Houben <stephan...@gmail.com>
wrote:

> What about using a simple decorator instead?
>
> def monkey_patch(cls):
>     return lambda func: setattr(cls, func.__name__, func)
>
> class Foo:
>    pass
>
> @monkey_patch(Foo)
> def bar(self):
>     return 42
>
> Foo().bar()
> # gives 42
>

This would work, but I still believe the proposed method is much shorter
and easier to follow. Decorator approach is no different from doing
`Foo.bar = bar` under the function definition I think, except it requires
one to figure out what the decorator does first.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to