1. What is introspection? Introspection is self-evaluation, self-reflection, self-criticism, self-control and self-education. It is a self-moral cultivation method proposed by Confucius. He said: "You can see your inspector and introspection ." (The Analects of Confucius) Of course, today we do not want to talk about the criticism and self-criticism of party members. Introspection is a kind of self-check 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 programmers with great flexibility and control.
This article introduces the introspection capability of the Python programming language. The entire Python language provides in-depth and extensive support for introspection. In fact, it is hard to imagine what the Python language looks like without its introspection feature.
2. When we enter the Python terminal IDE for python help, we can see help:
zhouyl@zhouyl:~/repository/blog/python$ pythonPython 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2Type "help", "copyright", "credits" or "license" for more information.
In the last line, we can enter help to get more information.
>>> Help # After Entering help, the system prompts you to enter help () to enter the interactive help interface, or use help (obj) to obtain the help information Type help () of obj () for interactive help, or help (object) for help about object. >>> help () # Enter help () Welcome to Python 2.7! This is the online help utility. if this is your first time using Python, you shoshould 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> # enter a description to enter the interactive help interface (Ctrl + C or enter quit to Exit) help> list # enter the list and enter another window similar to "man". the complete introduction is listhelp> keywords # Enter keywords to print only the list of python keywords Here is a list of the Python keywords. enter any keyword to get more help. and elif if printas else import raiseassert into T in returnbreak exec is tryclass finally lambda whilecontinue for not withdef from or yielddel global pass help> if # enter the keyword if, it is also similar to the above list in another window to introduce if
Of course, we can also use help (object) in python IDE to obtain help information for an object, such:
>>> Help (OS) # Want to see the OS module? ^ _ ^. No. You haven't loaded Traceback (most recent call last): File"
", Line 1, in
NameError: name 'OS' is not defined >>> import OS >>> help (OS) # After loading, you can view the OS help information >>>
3. Why is the sys module frequently used? The python official website introduces "System-specific parameters and functions" (System-specific parameters and functions ). Therefore, the sys module provides detailed internal information about Python. Import the module and use the dot (.) symbol to reference its content (such as variables, functions, and classes. The sys module contains various variables and functions that reveal interesting details about the current Python interpreter.
The platform variable tells us what operating system is in: sys. platform attribute
>>> import sys>>> sys.platform'linux2'
In the current Python, the version is represented by strings and metadata (tuples contain object sequences:
>>> sys.version'2.7.3 (default, Jan 2 2013, 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: sys. maxint attribute.
>>> sys.maxint2147483647
The argv variable is a list containing command line parameters (if the parameter is specified ). The first argv [0] is the path of the script to be run. When we run Python interactively, this value is an empty string: sys. argv attribute
>>> sys.argv['']
For the use of sys. argv, we have introduced the function learning notes in the previous section. It can be used to receive the function script, for example:
#!/usr/bin/pythonimport sysprint"The script name is "+sys.argv[0]print"The first parameter is %s" % sys.argv[1]print"The second parameter is %r" % sys.argv[2]
When using a script, we can bring parameters to the script:
Long @ zhouyl:/tmp/test $ python arg. py 1 2 3 # although the program only requires printing the first two parameters, we can also bring more, but it is not printed.
The script name is arg.pyThe first parameter is 1The second parameter is '2'
The path variable is the module search path. During the import, Python searches for the module in the directory list. : Sys. path attribute
>>> Sys. path ['', '/home/pobrien/Code', # The first empty string'' indicates 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 mounted modules to the module objects. As you can see, by default, Python loads certain modules: sys. modules attributes
>>> sys.modules{'stat':
,'__future__':
,'copy_reg':
,'posixpath':
,'UserDict':
,'signal':
,'site':
,'__builtin__':
,'sys':
,'posix':
,'types':
,'__main__':
,'exceptions':
,'os':
,'os.path':
}
4. When reading dive into python, Chapter 4 introduces 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 (lambda s: " ".join(s.split())) or (lambda s: s) print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList])
It is easy to use, as shown below:
>>> 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...>>> import 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. Several built-in functions are introduced, which use the following functions:
Dir ([obj]):
The dir () function may be the most famous part of the Python introspection mechanism. It returns an ordered list of the attribute names of any objects passed to it (some special attributes are excluded ). If no object is specified, dir () returns the name in the current scope (obj defaults to the current module object ).
Eg.
>>> li[1, 4, 8, 16, 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 a built-in function that has no confidence (the core of introspection) and can return any attribute of any object. Calling this method will return the attribute value named attr in obj. For example, if attr is 'bar', obj. bar is returned. Using the getattr function, you can get a reference to a function that is not known until the runtime. 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, 16, 2]>>> getattr(li,'append')(3)>>> li[1, 4, 8, 16, 2, 3]
Similar:
Hasattr (obj, attr ):
This method is used to check whether obj has an attribute named attr and returns a Boolean value.
Setattr (obj, attr, val ):
To call this method, the attribute named attr of obj is assigned val. For example, if attr is 'bar', it is equivalent to obj. bar = val.
Callable (obj ):
Objects that indicate potential behaviors (functions and methods) can be called. You can use the callable () function to test the callability of the parameter object. If the parameter object is callable, True is returned. 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 spaces to conform to the specified length. If the specified length is smaller than the string length (len (string), ljust will return a string that has not changed and will not be truncated.
Eg.
>>> s = "Zhou">>> print s.ljust(10)+"Hello"Zhou Hello>>> print s.ljust(1)+"Hello"ZhouHello
4.2. List parsing Python provides powerful list parsing capabilities to map a list to other lists. List Parsing is used in combination with the filtering mechanism, so that some elements in the list are mapped and other elements are skipped.
List parsing:
[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. The last step is to add a filter expression starting with if (the filter expression can be any expression that returns true or false values, in Python, there is almost anything ).
Note: If you carefully check the above apihelper. py instance, you can find that the author places one line of command in multiple lines. Can this problem be solved? Here, we can tell you that "list parsing can split an expression into multiple rows", because the entire expression is enclosed in square brackets.
4.3 The analysis module does not mention some usage of the above modules in the previous two sections. For example, and-or usage and lambda functions are all involved in the previous study notes of this series.
The above modules are analyzed one by one:
Def info (object, spacing = 10, collapse = 1 ):
# First, we can see that the defined function uses three parameters:
@ Object -- of course, it is the target we want to view. This function uses dir to obtain the object attributes, 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. A key parameter is defined here. That is to say, if spacing is not assigned a value when the info function is called, spacing uses the default value of 10. So what is this spacing? Look down, right! Yes, it gives ljust parameters! That is to say, strings with less than 10 characters will be filled with spaces, and those with more than 10 characters will not be modified.
@ Collapse = 1. A key parameter is set here. If no value is assigned, the default value is 1. Collapse is used as a judgment statement for and-or usage in the function.
"Print methods and doc strings.
Takes module, class, list, dictionary, or string ."""
# What is this? That's right. Of course it's docstring.
MethodList = [method for method in dir (object) if callable (getattr (object, method)]
# Use the Filter list described above. First, use dir to parse the object to obtain a list of all the attributes of the object, map it using list resolution, and filter it using if, the filter condition is object. method can be called! Therefore, the final methodList is a list of all callable attributes of an object.
ProcessFunc = collapse and (lambda s: "". join (s. split () or (lambda s: s)
# This sentence defines a function. For a lambda function with only one row, use the third parameter as the judgment for and-or usage. If collapse is True, processFunc = lambda s :"". join (s. split (), that is, when the split function is used to separate s first "". join is connected by space. If collapse is False, processFunc = lambda s: s, that is, s is not processed.
Print "\ n". join (["% s" %
(Method. ljust (spacing ),
ProcessFunc (str (getattr (object, method). _ doc __)))
For method in methodList])
# Here is a typical case of using list resolution in several rows. First, use method to traverse the methodList list (list of all callable attributes of the object obtained above ), when using processFunc to object. the _ doc _ (docstring) of method is processed and printed together with the method name adjusted by ljust. Use "\ n". join to separate different method information and print the information in different rows.
5. Print the available information of some functions.
The following is the introspection method used in the Python introspection guide. Some built-in functions (such as id (), type (), repr () are used to print object 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. The usage is as follows:
>>> Import apihelper # load our apihelper> apihelper.info (apihelper) # Call the previous info function to check the attributes of info Print methods and doc strings in apihelper. takes module, class, list, dictionary, or string. interrogate Print useful information about item. >>> apihelper. interrogate (apihelper) # Use interrogate to view our apihelper information NAME: apihelperCLASS: moduleID: 3075144100 TYPE:
VALUE:
CALLABLE: NoTraceback (most recent call last): File"
", Line 1, in
File "apihelper. py ", line 30, in interrogate doc = doc. strip () # Remove leading/trailing whitespace. attributeError: 'nonetype 'object has no attribute 'strip' >>> apihelper. interrogate (apihelper.info) # view apihelper.info information NAME: infoCLASS: functionID: 3075150932 TYPE:
VALUE:
CALLABLE: YesDOC: Print methods and doc strings. >>> apihelper. interrogate (123) # view integer Information CLASS: intID: 153002672 TYPE:
VALUE: 123 CALLABLE: NoDOC: int (x [, base])-> integer >>> apihelper. interrogate ("Hello") # view the string Information CLASS: strID: 3075198208 TYPE:
VALUE: 'hello' CALLABLE: NoDOC: str (object)-> string
So far, our introspection module apihelper has powerful functions, and its introspection capability is also good ~~~
==================================
Reference:
[1] http://www.ibm.com/developerworks/cn/linux/l-pyint/index.html? Ca = drs-# ibm-pcon
[2] dive into python-section4: (read online)
Http://woodpecker.org.cn/diveintopython/power_of_introspection/index.html
[3] http://www.cnblogs.com/huxi/archive/2011/01/02/1924317.html