top | item 24384775

(no title)

wrmsr | 5 years ago

As far as I can tell functools.partial has been implemented in C since it was added sixteen years ago ( https://github.com/python/cpython/commit/9c323f8de4910dfc0ba... ), and it's faster than lambda ( https://gist.github.com/wrmsr/1e13eda3ed78288679c010acbe6d2b... ). Regarding docstrings you can access the underlying function via the __wrapped__ attribute, and signature forwarding is too brittle and magical for stdlib.

discuss

order

BiteCode_dev|5 years ago

> As far as I can tell functools.partial has been implemented in C since it was added sixteen years ago ( https://github.com/python/cpython/commit/9c323f8de4910dfc0ba.... ), and it's faster than lambda ( https://gist.github.com/wrmsr/1e13eda3ed78288679c010acbe6d2b.... )

I tried to prove you wrong and run multiple tests that all shown you were right. Then I tried to run them on different python versions, down to Python 2.7, expecting that my knowledge was just dated.

No, I was just wrong.

Don't know why I was so convinced of this.

> Regarding docstrings you can access the underlying function via the __wrapped__ attribute,

This assumes one knows about that. A lot of people never even heard of partial(), and won't know about _ _wrapped_ _. Not to mention I never seen a tool using help() fetching _ _wrapped_ _._ _doc_ _ if it exists.

> and signature forwarding is too brittle and magical for stdlib.

@functools.wraps() does signature forwarding to great success, and is in the stdlib.

Nevertheless, I don't think it would be the right strategy for partial(), because you don't have the exact same signature. But at least having something like:

   This function is the result of x() being wrapped with partial(x, 1, foo='bar').

   Here is the original docstring of x():
   
    ....