I ended up reading PEP 362 --Function Signature Object to understand where the attribute __signature__
came from. So make a note of your understanding.
Signature
to represent function signatures so that function signatures can be represented simply.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