Re: [Python-Dev] Updated PEP 362 (Function Signature Object)

2012年6月07日 20:27:37 -0700

On Fri, Jun 8, 2012 at 12:18 PM, Larry Hastings <[email protected]> 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  |  [email protected]  |  Brisbane, Australia
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to