[Python-Dev] PEP 362 Third Revision (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Thu Jun 14 14:15:20 CEST 2012


Looks great!

One very minor quibble is that I prefer 'ns' to 'dct' for the namespace parameter in a metaclass, but that doesn't really matter for the PEP.

-- Sent from my phone, thus the relative brevity :) On Jun 14, 2012 9:45 PM, "Yury Selivanov" <yselivanov at gmail.com> wrote:

On 2012-06-14, at 12:17 AM, Nick Coghlan wrote:

> On Thu, Jun 14, 2012 at 1:06 PM, Yury Selivanov <yselivanov at gmail.com> wrote: >> On 2012-06-13, at 10:52 PM, Yury Selivanov wrote: >>> 2. signature() function support all kinds of callables: >>> classes, metaclasses, methods, class- & staticmethods, >>> 'functools.partials', and callable objects. If a callable >>> object has a 'signature' attribute it does a deepcopy >>> of it before return. >> >> >> Properly decorated functions are also supported. > > I'd like to see the "shared state" decorator from the previous thread > included, as well as a short interactive interpreter session showing > correct reporting of the signature of functools.partial instances.

OK. Below is how I want to update the PEP. Do you want to include anything else? Visualizing Callable Objects' Signatures ---------------------------------------- Let's define some classes and functions: :: from inspect import signature from functools import partial, wraps class FooMeta(type): def new(mcls, name, bases, dct, *, bar:bool=False): return super().new(mcls, name, bases, dct) def init(cls, name, bases, dct, **kwargs): return super().init(name, bases, dct) class Foo(metaclass=FooMeta): def init(self, spam:int=42): self.spam = spam def call(self, a, b, *, c) -> tuple: return a, b, c def sharedvars(*sharedargs): """Decorator factory that defines shared variables that are passed to every invocation of the function""" def decorator(f): @wraps(f) def wrapper(*args, **kwds): fullargs = sharedargs + args return f(*fullargs, **kwds) # Override signature sig = wrapper.signature = signature(f) for _ in sharedargs: sig.parameters.popitem(last=False) return wrapper return decorator @sharedvars({}) def example(state, a, b, c): return state, a, b, c def formatsignature(obj): return str(signature(obj)) Now, in the python REPL: :: >>> formatsignature(FooMeta) '(name, bases, dct, *, bar:bool=False)' >>> formatsignature(Foo) '(spam:int=42)' >>> formatsignature(Foo.call) '(self, a, b, *, c) -> tuple' >>> formatsignature(Foo().call) '(a, b, *, c) -> tuple' >>> formatsignature(partial(Foo().call, 1, c=3)) '(b, *, c=3) -> tuple' >>> formatsignature(partial(partial(Foo().call, 1, c=3), 2, c=20)) '(*, c=20) -> tuple' >>> formatsignature(example) '(a, b, c)' >>> formatsignature(partial(example, 1, 2)) '(c)' >>> formatsignature(partial(partial(example, 1, b=2), c=3)) '(b=2, c=3)' - Yury -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120614/ba5980ce/attachment-0001.html>



More information about the Python-Dev mailing list