Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Comments PTS 13] Rethinking Python decorators #13

Open
joaomcteixeira opened this issue Jul 8, 2020 · 6 comments
Open

[Comments PTS 13] Rethinking Python decorators #13

joaomcteixeira opened this issue Jul 8, 2020 · 6 comments

Comments

@joaomcteixeira
Copy link
Member

joaomcteixeira commented Jul 8, 2020

Original post: https://pythonicthoughtssnippets.github.io/2020/08/09/PTS13-rethinking-python-decorators.html
Post on DEV: https://dev.to/joaomcteixeira/rethinking-python-decorators-2g53

@joaomcteixeira joaomcteixeira changed the title [Comments PTS 13] Redifining Decorators [Comments PTS 13] Rethinking the decorator concept Jul 14, 2020
@caseyclements
Copy link

Thank you for this post! It was precisely what I needed. I was able to refactor an existing library containing a combination of nested lambdas, decorators, and inner functions using plain pickle, and then distribute using dask.

@joaomcteixeira joaomcteixeira changed the title [Comments PTS 13] Rethinking the decorator concept [Comments PTS 13] Rethinking Python decorators May 24, 2021
@joaomcteixeira
Copy link
Member Author

Hi @caseyclements !

Thanks very much for your comment. I am really happy you could use that strategy to refact on your side. Not much people talk about these intricate topics and there is definitively a leap from using python to write scripts and using python to write whole and structured programs.

Have you encountered any difficulties or additions beyond what I refer in my post? Glad to discuss further 👍

Cheers,

@bernstei
Copy link

bernstei commented Dec 8, 2021

Thanks for the interesting explanation. Using your functools.partial idea I was able to get a convenient pickleable syntax for a decorator with arguments. However, can you think of any way to get the docstring for the resulting function to be informative, rather than just the meaningless docstring for partial that results?

@joaomcteixeira
Copy link
Member Author

Hi @bernstei, thanks so much for your feedback. It really means a lot to me you enjoyed the post and that it was useful for you. Replacing the partialized function with the original docstring would be "a lie" because some parameters are already input. However, you can actually rewrite the partialized function docstring easily:

pfunc = functools.partial(myfunc, arg1=10)
pfunc.__doc__ = """Does what myfunc does but with arg1 = 10""" # you can go multiline here.
help(pfunc)  # shows you the new docstring.

Cheers,

@mutricyl
Copy link

Thanks indeed for your work. I needed a multiprocessing safe "decorator". The partial strategy works great !

@joaomcteixeira
Copy link
Member Author

Many thanks @mutricyl for your feedback. All the best!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants