[issue44003] functools.lru_cache omits __defaults__ attribute from wrapped function

Raymond Hettinger report at bugs.python.org
Sat May 1 21:29:01 EDT 2021


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

I don't really like it.  Carrying forward these attributes isn't the norm for wrapping functions.  

The __defaults__ argument is normally only used where it has an effect rather than in a wrapper where it doesn't.  Given that it is mutable, it invites a change that won't work.  For example:

    >>> def pow(base, exp=2):
        return base ** exp

    >>> pow.__defaults__
    (2,)
    >>> pow.__defaults__ = (3,)
    >>> pow(2)                     
    8

Also, an introspection function can only meaningfully use defaults when accompanied by the names of the fields:

    >>> pow.__code__.co_varnames
    ('base', 'exp')

However, these aren't visible by directly introspecting the wrapper.

FWIW, we've never had a user reported issue regarding the absence of __defaults__.  If ain't broke, let's don't "fix" it.

Nick and Serhiy, any thoughts?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44003>
_______________________________________


More information about the Python-bugs-list mailing list