A brief analysis of Python's powerful introspection

Source: Internet
Author: User
Tags iterable posix

1. What is introspection?

Introspection is self-evaluation, self-reflection, self-criticism, ego-control and self-education, which is a kind of self-moral cultivation method presented by Confucius. He said: "Emulate Yan, see not yin and internal introspection also." ("The Analects of Confucius and Leezen") of course, we do not want to talk about the criticism of party members today. To indicate the meaning, introspection (introspection) is a self-examination behavior. In computer programming, introspection refers to the ability to examine something to determine what it is, what it knows, and what it can do. Introspection provides the programmer with great flexibility and control.
This article describes the self-reflection capabilities of the Python programming language. The entire Python language provides deep and broad support for introspection. In fact, it's hard to imagine what the Python language would look like without its introspective nature.


2. Help for Python

When we get to Python's terminal IDE, we can see the help figure:

[Email protected]:~/repository/blog/python$ pythonpython 2.7.3 (default, Jan  2, 16:53:07) [GCC 4.7.2] on linux2t Ype "Help", "copyright", "credits" or "license" for more information.

The last line is a clear explanation. We can enter help to get more information.

>>> help# input Help, the system prompts you to enter Help () to enter the interface of the interactive help, or you can use the request (obj  Help (object), about Object.>>> Help () # Input Help () Welcome to Python 2.7! The online Help utility. If This is your first time using Python, you should definitely check outthe tutorial on the Internet at Http://docs.python . org/2.7/tutorial/.  Enter the name of any module, keyword, or topic to get Help on Writingpython programs and using Python modules. To quit this help utility Andreturn to the interpreter, just type "quit".  To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics". Each module also comes with a one-line summaryof what it does; To list the modules whose summaries contain a given wordsuch as "spam", type "modules spam" .help> # after entering a section "description", enter interactive hel P Interface (CTRL + C or input quit) help> list# Enter the list after entering another window similar to "Man", Complete introduction to Listhelp> keywords#  Enter keywords to print only a list of Python keyword lists of this Python keywords. Enter any keyword to get more help.and elif if printas else               Import Raiseassert except in Returnbreak            EXEC is Tryclass finally Lambda whilecontinue For does withdef from or Yieldde L Global Pass help> if# Enter the keyword if, also similar to the above list in another window to introduce if


Of course, we can also use Help (object) under the Python IDE to get help information for an object, such as:

>>> Help (OS) # want to see the OS module? ^_^, no way, you haven't loaded yet. Traceback (most recent):  
3. SYS module

For the SYS module, the use of more frequent see also more, then what is it? This is what the Python website describes: "System-specific parameters and Functions" (System-specific parameters and functions). So the SYS module is a module that provides detailed, intrinsic information about the Python itself. Use the module by importing the module and referencing its contents (such as variables, functions, and classes) with a dot (.) symbol. The SYS module contains a variety of variables and functions that reveal interesting details about the current Python interpreter.


The platform variable tells us what operating system is now: Sys.platform property

>>> import sys>>> sys.platform ' linux2 '



In the current Python, the version is represented by a string and a tuple (the tuple contains the sequence of objects):

>>> sys.version ' 2.7.3 (default, Jan  2, 16:53:07) \N[GCC 4.7.2] ' >>> sys.version_ Infosys.version_info (major=2, minor=7, micro=3, releaselevel= ' final ', serial=0)


The maxint variable reflects the maximum available integer value: Sys.maxint property

>>> sys.maxint2147483647

The argv variable is a list that contains the command-line arguments, if the parameter is specified. The first item, Argv[0], is the path to the running script. When we run Python interactively, this value is an empty string: sys.argv property

>>> sys.argv["]


With regard to the use of SYS.ARGV, we have described in the previous study notes on functions, which can be used to receive scripts for functions such as:

#!/usr/bin/pythonimport Sysprint "The script name is" +sys.argv[0]print "the first parameter is%s"% sys.argv[1]print "the s Econd parameter is%r "% sys.argv[2]


When the script is used, we can bring the script into the parameters:
[Email protected]:/tmp/test$ python arg.py 1 2 3 # Although the program only requires the first 2 parameters to print, but we can also bring more, just not print it

The script name is arg.pythe first parameter are 1The second parameter is ' 2 '



The path variable is the module search path, and Python looks for the module in the directory list during import. : Sys.path Property

>>> sys.path[', '/home/pobrien/code ',            # The front empty string ' means the current directory '/usr/local/lib/python2.2 ', '/usr/local/lib/ Python2.2/plat-linux2 ', '/usr/local/lib/python2.2/lib-tk ', '/usr/local/lib/python2.2/lib-dynload ', '/usr/local/ Lib/python2.2/site-packages ']



The modules variable is a dictionary that maps the names of all the modules currently loaded into the module object. As you can see, by default, Python loads specific modules: Sys.modules property

>>> sys.modules{' stat ': <module ' stat ' = "from=" "' =" "usr=" "local=" "lib=" "python2.2=" "Stat.pyc ' =" "; ' __future__ ': <module ' __future__ ' = "from=" "' =" "usr=" "local=" "lib=" "python2.2=" "__future__.pyc ' =" ";, ' Copy_ Reg ': <module ' copy_reg ' = "" from= "" ' = "" usr= "" local= "" lib= "" python2.2= "" Copy_reg.pyc ' = "", ' Posixpath ': < Module ' posixpath ' = "from=" "' =" "usr=" "local=" "lib=" "python2.2=" "Posixpath.pyc ' =" ", ' userdict ': <module ' UserDict ' = "" from= "" ' = "" usr= "" local= "" lib= "" python2.2= "" Userdict.pyc ' = "", ' signal ': <module ' signal ' = "" ( built-in) = "", "Site": <module ' site ' = "" from= "" ' = "" usr= "" local= "" lib= "" python2.2= "" site.pyc "=" "," __ builtin__ ': <module ' __builtin__ ' = "(built-in) =" ", ' sys ': <module ' sys ' =" "(built-in) =" ";, ' POSIX ': < Module ' POSIX ' = "" (built-in) = "", ' Types ': <module ' types ' = "from=" "' =" "usr=" "local=" "lib=" "python2.2=" "types . PYc ' = "";, ' __main__ ': <module ' __main__ ' = "" (built-IN) = "", ' Exceptions ': <module ' exceptions ' = "" (built-in) = "", ' OS ': <module ' os ' = "" from= "" ' = "" usr= "" Local= "lib=" "python2.2=" "Os.pyc ' =" "," Os.path ": <module ' posixpath ' =" from= "" ' = "" usr= "" local= "" lib= "" Pyth on2.2= "Posixpath.pyc ' =" ";}

4. The Introspection module in

In reading dive into Python, the fourth chapter speaks of introspection, the code is as follows:

# Apihelper.pydef Info (object, spacing=10, collapse=1): "" "    Print methods and Doc strings.        Takes module, class, List, dictionary, or string.    "" " MethodList = [method for method in Dir (object) if callable (GetAttr (object, method))]    Processfunc = collapse and (lambd A s: "". Join (S.split ())) or (lambda s:s)    print "\ n". Join (["%s%s"%                      (method.ljust (spacing),                       processfunc ( STR (GetAttr (object, method))))) (for                     method in MethodList])


It is also very convenient to use, as follows:

>>> from apihelper import info>>> li = []>>> info (li) __add__    x.__add__ (y) <==> X+y_ _class__  list (), New empty list List (iterable), new list initialized from iterable ' s items...>>> im Port apihelper>>> Info (apihelper) Info       Print methods and Doc strings. Takes module, class, List, dictionary, or string.>>> info (apihelper,20) Info                 Print methods and Doc strings. Takes module, class, List, dictionary, or string.>>> info (apihelper,20,0) Info                 Print methods and Doc strings. Takes module, class, List, dictionary, or string.

4.1. Introduction of several built-in functions

Several functions are used, mainly described as follows:
Dir ([obj]):
The Dir () function is probably the most famous part of the Python introspection mechanism. It returns a sorted list of the property names of any objects passed to it (some special attributes are not included). If you do not specify an object, dir () returns the name in the current scope (the default value of obj is the current module object).

eg.

>>> li[1, 4, 8, 2, 3]>>> dir (li) [' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem_ _ ', ' __delslice__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __getslice__ ', ' __ Gt__ ', ' __hash__ ', ' __iadd__ ', ' __imul__ ', ' __init__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mul__ ', ' __ne__ ', ' _ _new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __reversed__ ', ' __rmul__ ', ' __setattr__ ', ' __setitem__ ', ' __ setslice__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' append ', ' count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ']


GetAttr (obj, attr):
GetAttr is an incredibly useful built-in function (the core of introspection) that can return any property of any object. Calling this method returns the value of the property named attr value in obj, such as Obj.bar if attr is ' bar '. Using the GetAttr function, you can get a reference to a function that does not know the name until run time. The return value of GetAttr is a method that can be called.

eg.

>>> li[1, 4, 8, 16]>>> li.append (2) >>> li[1, 4, 8,, 2]>>> getattr (li, ' Append ') (3) & Gt;>> li[1, 4, 8, 16, 2, 3]



There are also similar:
Hasattr (obj, attr):
This method is used to check if obj has a property named attr value, and returns a Boolean value.
SetAttr (obj, attr, val):
Calling this method assigns a value of Val to the property of obj named attr. For example, if attr is ' bar ', it is equivalent to Obj.bar = val.


Callable (obj):
You can invoke an object that represents the potential behavior (functions and methods). The callable () function can be used to test the invocation of a Parameter object, which can be called to return true, otherwise false is returned.

eg.

>>> li[1, 2, 4, 8, 16]>>> callable (LI) false>>> callable (li.append) True

String.ljust (length):
Ljust fills the string with a space to match the specified length. If the specified length (length) is less than the length of the string (len (String)), Ljust will simply drop the unchanged string and not truncate the string.

eg.

>>> s = "Zhou" >>> print S.ljust (ten) + "Hello" Zhou      hello>>> print s.ljust (1) + "Hello" Zhouhello

4.2. List resolution

Python has powerful list parsing capabilities that will map the list to other lists. List parsing is used in conjunction with the filtering mechanism to make certain elements in the list be mapped and skipped over other elements.
List resolution:
[Mapping-expression for item in Source-list]
eg.
>>> li = [1,2,4,8,16]
>>> [item*2 for item in Li]
[2, 4, 8, 16, 32]
Filter List Syntax:
[Mapping-expression for item in Source-list if Filter-expression]
eg.
>>> li = [1,2,4,8,16]
>>> [item*2 for item in Li if item%2 = = 0]
[4, 8, 16, 32]
As shown above, the filter list is an extension of list resolution, the first three parts are the same, and the last step is to add a filter expression at the beginning of the if (the filter expression can be any expression that returns a value of TRUE or false, and almost anything in Python).


Note: If you look closely at the apihelper.py instance above, you can see that the author put a line of commands on multiple lines, which is OK? What you can tell you here is that "list parsing can be split into multiple rows with an expression," because the entire expression is enclosed in a square bracket.

4.3 Analysis module

There are also some uses in the above modules that are not mentioned in the above two sections. For example, and-or usage and lambda functions, which are covered in the previous study notes in this series, are not described in this article.


The following step-by-step analysis of the above modules:
def info (object, spacing=10, collapse=1):
# First, we can see that the defined function uses three parameters:
@ Object--Of course the target we want to see, this function uses Dir to get the property of object, and because the Dir function is powerful, the object here can be anything in Python, including Module object, function object, string object, List object, Dictionary object ...
@ spacing = 10, where a key parameter is defined, that is, if the info function is called without assigning a value to spacing, spacing will use the default of 10. So what's this spacing for? Look down, right! Yes, it's a parameter for Ljust! That is, less than 10 of the string will use space to fill the gas, and more than 10 do not modify
@ collapse = 1, a key parameter is also set, and if not assigned, the default value of 1 is used. Collapse is used as a judgment statement for and-or usage in a function.
"" "Print methods and Doc strings.


Takes module, class, List, dictionary, or string. "" "
# What is this? Yes, of course it's docstring.
MethodList = [method for method in Dir (object) if callable (GetAttr (object, method)]
# using the filter list described above, first use Dir to parse object to get a list of all properties of object, use list resolution to map it and filter it by if, filter condition is Object.Method callable! So, the last MethodList is a list of all the callable properties of an object
Processfunc = Collapse and (lambda s: "". Join (S.split ())) or (lambda s:s)
# This sentence defines a function, yes, only a single line of lambda functions, and uses the third argument as a judgment of the and-or usage. If collapse is True,processfunc = lambda s: "". Join (S.split ()), also will s first use the Split function to split in using "". Join joins it using spaces. If collapse is False,processfunc = Lambda s:s, it does not do any processing on s
print "\ n". Join (["%s%s"%
(Method.ljust (spacing),
Processfunc (str (GetAttr (object, method). __doc__)))
For method in MethodList])
# here is a typical case where the list parsing is used in a few lines, first using method to traverse the list of methodlist (all the callable properties of the object obtained above), in the use of Processfunc to Object.Method _ _DOC__ (docstring) is processed and printed together with the method name that is adjusted with Ljust. and use "\ n". Join divides each different method information, dividing it into different lines of print.

5. Print out the available information for some functions

Here is the introspection method used in the Python introspection guide, which uses some of the built-in functions (ID (), type (), repr (), and so on) to print the object's information:

def interrogate (item): "" "    Print useful information about item.    " " If Hasattr (item, ' __name__ '):        print "Name:    ", item.__name__    if Hasattr (item, ' __class__ '):        print " CLASS:   ", item.__class__.__name__    print" ID:      ", ID (item)    print" type:    ", type (item)    Print "VALUE:   ", repr (item)    print "callable:",    if Callable (item):        print "Yes"    else:        print " No "    if Hasattr (item, ' __doc__ '):        doc = getattr (item, ' __doc__ ')    doc = Doc.strip ()   # Remove leading/ Trailing whitespace.    Firstline = Doc.split (' \ n ') [0]    print "Doc:     ", Firstline



We add it to the Apihelper module as another method of our introspection module, which is used as follows:

>>> Import Apihelper # Load our apihelper>>> apihelper.info (apihelper) # Call the previous info function to view Apihelper What properties are in Info Print methods and Doc strings. Takes module, class, List, dictionary, or string.interrogate Print useful information about item.>>> apihelper.in Terrogate (apihelper) # Use interrogate to view our Apihelper information Name:apihelperclass:moduleid:3075144100type: & Lt;type ' module ' = ">value: <module ' apihelper ' =" "from=" "' apihelper.py ' =" ">callable:notraceback (most Recen T call last): File "", line 1, in File "apihelper.py", line A, in interrogate doc = Doc.strip () # Remove leading/ Trailing whitespace. Attributeerror: ' Nonetype ' object has no attribute ' strip ' >>> apihelper.interrogate (apihelper.info) # view Apihel Per.info information name:infoclass:functionid:3075150932type: <type ' function ' = ' "" >value:callable:yesd  Oc:print methods and Doc strings.>>> apihelper.interrogate (123)            # View integer information class:intid:153002672type: <type ' int ' = ' "" >value:123callable:nodoc:int (x[, Base]), Integer>>> apihelper.interrogate ("Hello") # View the information for the string class:strid:3075198208t YPE: <type ' str ' = "" >value: ' Hello ' CALLABLE:NoDOC:str (object), string


At this point, our introspection module Apihelper also has a relatively powerful function, its introspection ability is also good ~ ~ ~

A brief analysis of Python's powerful introspection

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.