18, "reprint" Python in the Inspect module

Source: Internet
Author: User
Tags python script

Reprinted from: 53053762

Objective

I learned the actual combat Day5-python Tutorial-Liaoche's official website, encountered the inspect module, before the inspect module ignorant ah, so in the spirit of breaking the sand pot asked the end, decided to do some research on the inspect module.

According to degree Niang search, inspect module mainly provides four kinds of use:

(1). Type check for modules, frames, functions, etc.

(2). Get the source code

(3). Get information about the parameters of a class or function

(4). Parsing the Stack

In this course, I used only the third use, that is, to get the information about the parameters of the class or function, let me explore.

Explore

In combination with the course I am studying, I have also done some research on inspect myself. Based on some of the functions and methods used in the course, I made a Python script.

#TestImportInspectdefFN (A, b=0, *c, D, E=1, f='Hello', **g):PassSig=inspect.signature (FN)Print("the value of Inspect.signature (FN) is:%s"% sig)#(A, b=0, *c, D, e=1, f= ' Hello ', **g)Print("type of Inspect.signature (FN):%s"% (Type (SIG)))#<class ' inspect. Signature ' >Print("\ n") Params=sig.parametersPrint("the value of the Signature.paramerters property is:%s"% params)#ordereddict (' A ', <parameter ' a ' >), (' B ', <parameter "b=0" >), (' C ', <parameter "*c" >), (' d ', < Parameter "D" >), (' E ', <parameter "e=1" >), (' F ', <parameter "f= ' Hello '" >), (' G ', <parameter "**g" >) ])Print("the type of the Ignature.paramerters property is%s"% type (params))#<class ' mappingproxy ' >Print("\ n") forName, paraminchParams.items ():Print('*'* 20,'Start','*'* 20)    Print("the two values returned by Mappingproxy.items () are:%s and%s"%(name, param))Print("Mappingproxy.items () the type of two values returned is:%s and%s"% (type (name), type (param)))#<class ' str ' > and <class ' Inspect. Parameter ' >    Print("\ n") K=Param.kindPrint("the value of the Parameter.kind property is:%s"%k)Print("the type of the Parameter.kind property is:%s"% type (k))#<enum ' _parameterkind ' >    Print("\ n") DFT=Param.defaultPrint("the value of the Parameter.default is:%s"%DfT)Print("the property of the Parameter.default is:%s"%type (DFT))Print('*'* 20,'End','*'* 20)    Print("\ n") PK=Inspect. Parameter.positional_or_keywordPrint("Inspect. The value of the Parameter.positional_or_keyword is:%s"%PK)Print("Inspect. The type of Parameter.positional_or_keyword is:%s"%type (PK)) Ko=Inspect. Parameter.keyword_onlyPrint("Inspect. The value of the parameter.keyword_only is:%s"%KO)Print("Inspect. The type of parameter.keyword_only is:%s"% type (KO))

Executing the above script will give the following output:

The values for Inspect.signature (FN) are: (A, b=0, *c, D, E=1, f='Hello', **g) Type of Inspect.signature (FN):<class 'Inspect. Signature'>the value of the Signature.paramerters property is: Ordereddict ([('a', <parameter"a">), ('b', <parameter"b=0">), ('C', <parameter"*c">), ('D', <parameter"D">), ('e', <parameter"e=1">), ('F', <parameter"f= ' Hello '">), ('g', <parameter"**g">)]) the type of the Ignature.paramerters property is<class 'Mappingproxy'>******************** Start ********************The two values returned by Mappingproxy.items () are: A and amappingproxy.items () are the types of the two values returned, respectively:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Positional_or_keywordparameter.kind property is:<enum'_parameterkind'>the value of the Parameter.default is:<class 'Inspect._empty'>the properties of the Parameter.default are:<class 'type'>******************** End **************************************** Start ********************the two values returned by Mappingproxy.items () are: B and B, respectively.=The types of two values returned by 0mappingproxy.items () are:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Positional_or_keywordparameter.kind property is:<enum'_parameterkind'>The value of the Parameter.default is: The 0parameter.default property is:<class 'int'>******************** End **************************************** Start ********************the two values returned by Mappingproxy.items () are: C and*The types of two values returned by Cmappingproxy.items () are:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Var_positionalparameter.kind property is:<enum'_parameterkind'>the value of the Parameter.default is:<class 'Inspect._empty'>the properties of the Parameter.default are:<class 'type'>******************** End **************************************** Start ********************The two values returned by Mappingproxy.items () are: the types of two values returned by D and Dmappingproxy.items () are respectively:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Keyword_onlyparameter.kind property is:<enum'_parameterkind'>the value of the Parameter.default is:<class 'Inspect._empty'>the properties of the Parameter.default are:<class 'type'>******************** End **************************************** Start ********************the two values returned by Mappingproxy.items () are: E and e=1The types of two values returned by Mappingproxy.items () are:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Keyword_onlyparameter.kind property is:<enum'_parameterkind'>the value of the Parameter.default is:1the properties of the Parameter.default are:<class 'int'>******************** End **************************************** Start ********************the two values returned by Mappingproxy.items () are: F and f='Hello'The types of two values returned by Mappingproxy.items () are:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Keyword_onlyparameter.kind property is:<enum'_parameterkind'>The value of the Parameter.default is: The Helloparameter.default property is:<class 'Str'>******************** End **************************************** Start ********************the two values returned by Mappingproxy.items () are: G and**The types of two values returned by Gmappingproxy.items () are:<class 'Str'> and <class 'Inspect. Parameter'>The value of the Parameter.kind property is: The type of the Var_keywordparameter.kind property is:<enum'_parameterkind'>the value of the Parameter.default is:<class 'Inspect._empty'>the properties of the Parameter.default are:<class 'type'>******************** End ********************Inspect. The value of Parameter.positional_or_keyword is: the type of POSITIONAL_OR_KEYWORDinspect.Parameter.POSITIONAL_OR_KEYWORD is:<enum'_parameterkind'>Inspect. The value of parameter.keyword_only is: the type of KEYWORD_ONLYinspect.Parameter.KEYWORD_ONLY is:<enum'_parameterkind'>
Summarize
    • inspect.signature(fn)will return an inspect.Signature object of type, with the value FN all parameters of this function

    • inspect.SignatureAn object's paramerters property is an mappingproxy object of type (map), and the value is an ordered dictionary (orderdict).

      • The key in this dictionary is the name of the parameter, the str type

      • The value in this dictionary is a inspect.Parameter type of object, according to my understanding, this object contains a parameter of a variety of information

    • inspect.ParameterAn object's kind property is an _ParameterKind enumerated type of object, and the value is the type of the parameter (variable parameter, keyword parameter, etc)

    • inspect.ParameterProperties of the object default : If this parameter has a default value, it returns the default value, and if not, returns a inspect._empty class.

18, "reprint" Python in the Inspect module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.