[Python-Dev] PEP 362 Third Revision
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 shared_vars(*shared_args):
> """Decorator factory that defines shared variables that are
> passed to every invocation of the function"""
>
> def decorator(f):
> @wraps(f)
> def wrapper(*args, **kwds):
> full_args = shared_args + args
> return f(*full_args, **kwds)
> # Override signature
> sig = wrapper.__signature__ = signature(f)
> for __ in shared_args:
> sig.parameters.popitem(last=False)
> return wrapper
> return decorator
>
>
> @shared_vars({})
> def example(_state, a, b, c):
> return _state, a, b, c
>
>
> def format_signature(obj):
> return str(signature(obj))
>
>
> Now, in the python REPL:
>
> ::
>
> >>> format_signature(FooMeta)
> '(name, bases, dct, *, bar:bool=False)'
>
> >>> format_signature(Foo)
> '(spam:int=42)'
>
> >>> format_signature(Foo.__call__)
> '(self, a, b, *, c) -> tuple'
>
> >>> format_signature(Foo().__call__)
> '(a, b, *, c) -> tuple'
>
> >>> format_signature(partial(Foo().__call__, 1, c=3))
> '(b, *, c=3) -> tuple'
>
> >>> format_signature(partial(partial(Foo().__call__, 1, c=3), 2, c=20))
> '(*, c=20) -> tuple'
>
> >>> format_signature(example)
> '(a, b, c)'
>
> >>> format_signature(partial(example, 1, 2))
> '(c)'
>
> >>> format_signature(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