[PYTHON] I read PEP-362 (Function Signature Object) Memo

I ended up reading PEP 362 --Function Signature Object to understand where the attribute __signature__ came from. So make a note of your understanding.

Overview

Impressions

The rest is detailed usage, so I will omit the explanation. You can read PEP 362 or the [inspect page] in the Python documentation (https://docs.python.org/ja/3/library/inspect.html#introspecting-callables-with-the-signature-object). You may read. There used to be a past where ʻinspect.getargspec () and object attributes were used to read function signatures, but now ʻinspect.signature () is enough for most things. Viva, Signature.

Now, the first question, __signature__, was also mentioned in this PEP. If you save the Signature object in func.__signature__, ʻinspect.signature ()` will return that value. In other words, it's like a cache.

In PEP 362, C extension was excluded, but I scanned Argument Clinic How-To. As far as I can tell, it seems that creating a C extension using (and therefore) using the C file preprocessor Argument Clinic will create a module that supports Signature. It's convenient.

There aren't many situations where you need a function signature, but it's useful for investigating arguments and function types, so why not give it a try?

Recommended Posts

I read PEP-362 (Function Signature Object) Memo
I read PEP-593 (Flexible function and variable annotations)
I read PEP 613 (Explicit Type Aliases)
I read PEP 612 (Parameter Specification Variables)
I read PEP 604 (Complementary syntax for Union []).
I read PEP 618 (Add Optional Length-Checking To zip)
I read PEP 614 (Relaxing Grammar Restrictions On Decorators)
I read PEP-544 (Protocols: Structural subtyping (static duck typing)).
I read PEP 585 (Type Hinting Generics In Standard Collections)