As explained here, the Show
Edit: This was previously only used for built-in functions but since Python 3.8, you can use this in your own functions. The natural companion of
AbstractPython has always supported powerful introspection capabilities, including introspecting functions and methods (for the rest of this PEP, “function” refers to both functions and methods). By examining a function object you can fully reconstruct the function’s signature. Unfortunately this information is stored in an inconvenient manner, and is spread across a half-dozen deeply nested attributes. This PEP proposes a new representation for function signatures. The new representation contains all necessary information about a function and its parameters, and makes introspection easy and straightforward. However, this object does not replace the existing function metadata, which is used by Python itself to execute those functions. The new metadata object is intended solely to make function introspection easier for Python programmers. Signature ObjectA Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a
Parameter object in its A Signature object has the following public attributes and methods:
Signature objects are immutable. Use >>> def foo() -> None: ... pass >>> sig = signature(foo) >>> new_sig = sig.replace(return_annotation="new return annotation") >>> new_sig is not sig True >>> new_sig.return_annotation != sig.return_annotation True >>> new_sig.parameters == sig.parameters True >>> new_sig = new_sig.replace(return_annotation=new_sig.empty) >>> new_sig.return_annotation is Signature.empty True There are two ways to instantiate a Signature class:
It’s possible to test Signatures for equality. Two signatures are equal when their parameters are equal, their positional and positional-only parameters appear in the same order, and they have equal return annotations. Changes to the Signature object, or to any of its data members, do not affect the function itself. Signature also implements >>> str(Signature.from_function((lambda *args: None))) '(*args)' >>> str(Signature()) '()' Parameter ObjectPython’s expressive syntax means functions can accept many different kinds of parameters with many subtle semantic differences. We propose a rich Parameter object designed to represent any possible function parameter. A Parameter object has the following public attributes and methods:
Parameter constructor:
Two parameters are equal when they have equal names, kinds, defaults, and annotations. Parameter objects are immutable. Instead of modifying a Parameter object, you can use >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) >>> str(param) 'foo=42' >>> str(param.replace()) 'foo=42' >>> str(param.replace(default=Parameter.empty, annotation='spam')) "foo:'spam'" BoundArguments ObjectResult of a Has the following public attributes:
The
def test(a, *, b): ... sig = signature(test) ba = sig.bind(10, b=20) test(*ba.args, **ba.kwargs) Arguments which could be passed as part of either def test(a=1, b=2, c=3): pass sig = signature(test) ba = sig.bind(a=10, c=13) >>> ba.args (10,) >>> ba.kwargs: {'c': 13} ImplementationThe implementation adds a new function The function implements the following algorithm:
Note that
the An implementation for Python 3.3 can be found at [1]. The python issue tracking the patch is [2]. Design ConsiderationsNo implicit caching of Signature objectsThe first PEP design had a provision for implicit caching of
Some functions may not be introspectableSome functions may not be introspectable in certain implementations of Python. For example, in CPython, built-in functions defined in C provide no metadata about their arguments. Adding support for them is out of scope for this PEP. Signature and Parameter equivalenceWe assume that parameter names have semantic significance–two signatures are equal only when their corresponding parameters are equal and have the exact same names. Users who want looser equivalence tests, perhaps ignoring names of VAR_KEYWORD or VAR_POSITIONAL parameters, will need to implement those themselves. ExamplesVisualizing Callable Objects’ SignatureLet’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 @classmethod def spam(cls, a): return a 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, **kwargs): full_args = shared_args + args return f(*full_args, **kwargs) # Override signature sig = signature(f) sig = sig.replace(tuple(sig.parameters.values())[1:]) wrapper.__signature__ = sig 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(Foo.spam) '(a)' >>> 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)' Annotation Checkerimport inspect import functools def checktypes(func): '''Decorator to verify arguments and return types Example: >>> @checktypes ... def test(a:int, b:str) -> int: ... return int(a * b) >>> test(10, '1') 1111111111 >>> test(10, 1) Traceback (most recent call last): ... ValueError: foo: wrong type of 'b' argument, 'str' expected, got 'int' ''' sig = inspect.signature(func) types = {} for param in sig.parameters.values(): # Iterate through function's parameters and build the list of # arguments types type_ = param.annotation if type_ is param.empty or not inspect.isclass(type_): # Missing annotation or not a type, skip it continue types[param.name] = type_ # If the argument has a type specified, let's check that its # default value (if present) conforms with the type. if param.default is not param.empty and not isinstance(param.default, type_): raise ValueError("{func}: wrong type of a default value for {arg!r}". \ format(func=func.__qualname__, arg=param.name)) def check_type(sig, arg_name, arg_type, arg_value): # Internal function that encapsulates arguments type checking if not isinstance(arg_value, arg_type): raise ValueError("{func}: wrong type of {arg!r} argument, " \ "{exp!r} expected, got {got!r}". \ format(func=func.__qualname__, arg=arg_name, exp=arg_type.__name__, got=type(arg_value).__name__)) @functools.wraps(func) def wrapper(*args, **kwargs): # Let's bind the arguments ba = sig.bind(*args, **kwargs) for arg_name, arg in ba.arguments.items(): # And iterate through the bound arguments try: type_ = types[arg_name] except KeyError: continue else: # OK, we have a type for the argument, lets get the corresponding # parameter description from the signature object param = sig.parameters[arg_name] if param.kind == param.VAR_POSITIONAL: # If this parameter is a variable-argument parameter, # then we need to check each of its values for value in arg: check_type(sig, arg_name, type_, value) elif param.kind == param.VAR_KEYWORD: # If this parameter is a variable-keyword-argument parameter: for subname, value in arg.items(): check_type(sig, arg_name + ':' + subname, type_, value) else: # And, finally, if this parameter a regular one: check_type(sig, arg_name, type_, arg) result = func(*ba.args, **ba.kwargs) # The last bit - let's check that the result is correct return_type = sig.return_annotation if (return_type is not sig._empty and isinstance(return_type, type) and not isinstance(result, return_type)): raise ValueError('{func}: wrong return type, {exp} expected, got {got}'. \ format(func=func.__qualname__, exp=return_type.__name__, got=type(result).__name__)) return result return wrapper AcceptancePEP 362 was accepted by Guido, Friday, June 22, 2012 [3] . The reference implementation was committed to trunk later that day. ReferencesCopyrightThis document has been placed in the public domain. What does '/' mean in Python signature?In the case of __eq__(self, value, /) the slash is at the end, which means that all arguments are marked as positional only while in the case of your __init__ only self, i.e. nothing, is positional only.
What doesthe -> int just tells that f() returns an integer (but it doesn't force the function to return an integer). It is called a return annotation, and can be accessed as f. __annotations__['return'] . Python also supports parameter annotations: def f(x: float) -> int: return int(x)
What does a forward slash mean in Python?Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.
What does * mean Python function?It means that the function takes zero or more arguments and the passed arguments would be collected in a list called formulation . For example, when you call sum(1, 2, 3, 4) , formation would end up being [1, 2, 3, 4] .
|