Search code examples
pythonfunctools

Why does `functools.partial` not inherit `__name__` and other meta data by default?


I am wondering why meta data (e.g. __name__, __doc__) for the wrapped method/function by partial is not inherited by default. Instead, functools provides the update_wrapper function.

Why it is not done by default is not mentioned anywhere (as far as I could see) e.g. here and many tutorials on functools.partial talk about how to "solve the issue" of a missing __name__.

Are there examples where inheriting this information causes problems/confusion?


Solution

  • Think about what that would actually look like:

    def add(x, y):
        "Adds two numbers"
        return x + y
    
    add_5 = partial(add, 5)
    

    Would it actually make sense for add_5 to have __name__ set to "add" and __doc__ set to "Adds two numbers"?

    The callable created by partial behaves completely differently from the original function. It wouldn't be appropriate for the new callable to inherit the name and docstring of a function with completely different behavior.