[Python-Dev] Updated PEP 362 (Function Signature Object)
Nick Coghlan
ncoghlan at gmail.com
Fri Jun 8 05:25:39 CEST 2012
On Fri, Jun 8, 2012 at 12:18 PM, Larry Hastings <larry at hastings.org> wrote:
> On 06/07/2012 07:08 PM, Steven D'Aprano wrote:
>
> Perhaps func.__signature__ should be a computed the first time it is
> accessed?
>
>
> The PEP already declares that signatures are lazily generated. signature()
> checks to see if __signature__ is set, and if it is returns it. (Or,
> rather, a deepcopy of it, assuming we go down that route.) If __signature__
> isn't set, signature() computes it and returns that. (Possibly caching in
> __signature__, possibly not, possibly caching the result then returning a
> deepcopy.)
The need for a consistent return value ownership guarantee (i.e. "it's
OK to mutate the return value of inspect.signature()") and the
implicit inspect module guarantee of "introspection is non-intrusive
and doesn't affect the internal state of inspected objects" are the
main reasons I think implicit caching is a bad idea.
However, there are also a couple of pragmatic reasons I don't like the idea:
1. It's unlikely there will be a huge performance difference between
deep copying an existing Signature object and deriving a new one
2. If someone actually *wants* cached signature lookup for speed
reasons and is willing to sacrifice some memory in order to do so
(e.g. when using signatures for early parameter checking), they can
just do: "cached_signature = functools.lru_cache(inspect.signature)"
This approach will provide *genuine* caching, with no deep copying
involved when the same callable is encountered a second time. You will
also get all the benefits that the LRU caching mechanism provides,
rather than having a hidden unbounded caching mechanism that
potentially encompasses every function in the application.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list