1. What is introspection?
Self-reflection refers to self-evaluation, self-reflection, self-criticism, self-regulation and self-education. It is a method of self-moral cultivation proposed by Confucius.He said, "See the sage and think alike, see the sage and introspect yourself."(The Analects of Confucius Liren) Of course, we don't want to talk about criticism and self-criticism of Party members today.In other words, introspection is a self-checking 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.Self-reflection provides programmers with great flexibility and control.
This paper introduces the self-introspection of the Python programming language.The entire Python language provides deep and extensive support for the province.In fact, it's hard to imagine what a Python language would look like without its introspective nature.
2. Python help
When entering python's terminal IDE, we will see help:
zhouyl@zhouyl:~/repository/blog/python$ python Python 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
The last line clearly states that we can enter help for more information.
>>> help # After entering help, the system prompts you to enter help() into the interactive help interface, or use help(obj) to get help information for obj Type help() 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 should definitely check out the 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 writing Python programs and using Python modules. To quit this help utility and return 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 summary of what it does; to list the modules whose summaries contain a given word such as "spam", type "modules spam". help> # After entering a "description", enter the interactive help interface (Ctrl+C or quit to exit) help> list # Enter a list and enter another window like "man" to complete the list help> keywords # Enter keywords to print only a list of python keywords Here is a list of the Python keywords. Enter any keyword to get more help. and elif if print as else import raise assert except in return break exec is try class finally lambda while continue for not with def from or yield del global pass help> if # Enter the keyword if, which is similar to the list above introducing if in another window
Of course, we can also use help(object) under python's IDE to get help information for an object, such as:
>>> help(os) # Want to see the os module?^^, no door, you haven't loaded yet Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'os' is not defined >>> import os >>> help(os) # Once loaded, you can view os help information >>>
3. sys module
For the sys module, it is used more frequently and more frequently, so what is it?The Python website says, "System-specific parameters and functions" (system-specific parameters and functions).So the sys module is the module that provides detailed internal information about Python itself.Modules are used by importing them and referencing their contents (such as variables, functions, and classes) with dot (.) symbols.The sys module contains variables and functions that reveal interesting details about the current Python interpreter.
The platform variable tells us what operating system we are on: the sys.platform property
>>> import sys >>> sys.platform 'linux2'
In current Python,
Versions are represented as strings and tuples (tuples contain sequences of objects):
>>> sys.version '2.7.3 (default, Jan 2 2013, 16:53:07) \n[GCC 4.7.2]' >>> sys.version_info sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
The maxint variable reflects the largest integer value available:
sys.maxint attribute
>>> sys.maxint 2147483647
The argv variable is a list of command line parameters, if specified.The first argv[0] is the path to the script being run.When we interactively run Python, this value is an empty string: the sys.argv property
>>> sys.argv ['']
With regard to the use of sys.argv, we have described in the learning notes on the functions described earlier, which can be used to receive scripts for functions, such as:
#!/usr/bin/python import sys print"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 take parameters into the script:
Long@zhouyl:/tmp/test$Python arg.py 1 223 #Although only the first two parameters are required in the program to print, we can also take more, just not print
The script name is arg.py The first parameter is 1 The second parameter is '2'
The path variable is the module search path in which Python will look for modules in the catalog list during import.:
sys.path attribute
>>> sys.path ['', '/home/pobrien/Code', # The first empty string''refers to 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 modules currently loaded to module objects.As you can see, by default, Python loads certain 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/python2.2/posixpath.pyc'>}
4. <dive into python>introspective module
While reading dive into python, Chapter IV talks about introspection, coded as follows:
# apihelper.py def 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 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 ... >>> 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. Introduction to several built-in functions
Several functions are used, mainly as follows:
dir([obj])
:
The dir() function is probably the most famous part of Python's introspective mechanism.It returns a sorted list of attribute names of any objects passed to it (with some special attributes not included).If no object is specified, dir() returns the name in the current scope (the default value of obj is 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 an incredibly useful built-in function (the core of introspection) that returns any property of any object.Calling this method returns the value of an attribute named attr value in obj, such as obj.bar if attr is'bar'.Using the getattr function, you get a reference to a function whose name is unknown 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, 16, 2] >>> getattr(li,'append')(3) >>> li [1, 4, 8, 16, 2, 3]
Similarly, there are:
hasattr(obj, attr):
This method checks whether obj has an attribute with a value named attr and returns a Boolean value.
setattr(obj, attr, val):
Calling this method assigns a value to the attribute named attr of obj to val.For example, if attr is'bar', it is equivalent to obj.bar = val.
callable(obj)
:
Objects representing potential behavior (functions and methods) can be invoked.The callable() function can be used to test the callability of a parameter object, which returns True if called, or False if not.
eg.
>>> li [1, 2 , 4, 8, 16] >>> callable(li) False >>> callable(li.append) True
string.ljust(length)
:
Ljust Fills the string with spaces to match the specified length.If the specified length is less than the length of the string, ljust simply returns the unchanged string without truncating it.
eg.
>>> s = "Zhou" >>> print s.ljust(10)+"Hello" Zhou Hello >>> print s.ljust(1)+"Hello" ZhouHello
4.2. List parsing
Python has the power of list resolution to map lists to other lists.List resolution is used in conjunction with a filtering mechanism so that some elements in the list are mapped and others 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, a filter list is an extension of list parsing, with the first three parts being the same, and the last step adding a filter expression starting with if (a filter expression can be any expression that returns a true or false value, whereas almost anything in Python).
Note: If you look closely at the apihelper.py instance above, you can see that the author put one line of commands on more than one line, which is OK?What you can tell here is that "List parsing can split an expression into multiple lines" because the entire expression is enclosed in square brackets.
4.3 Progressive Module
There are also some uses for the above modules that are not mentioned in the previous two sections.For example, the and-or usage and lambda functions, which are covered in this series of Previous Study Notes, will not be repeated in this article.
The following is a sentence-by-sentence 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 object we want to see. This function uses dir to get the properties of an object, but because of the power of the dir function, the object here can be anything in python, including module objects, function objects, string objects, list objects, dictionary objects...
@ spacing = 10, which defines a key parameter, 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 does parameter ljust!That is, strings less than 10 will be filled with spaces and 10 will not be modified
@ collapse = 1, there is also a key parameter set here, if not assigned, use the default value of 1.Collapse is used as a judgment statement in the function for and-or usage.
"""Print methods and doc strings.
Takes module, class, list, dictionary, or string."""
#What is this?Yes, docstring of course
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 get a list of all the properties of the object, use list parsing to map it, and filter it through if, the filter condition is object.method callable!Therefore, the resulting methodList is a list of all the callable properties of an object
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
The #clause defines a function, a lambda function with only one line, and uses the third parameter as a judgment of and-or usage.If collapse is True, processFunc = lambda s:''.join (s.split())', that is, s is first split using the split function and joined using a space using''.join'.If collapse is False, processFunc = lambda s: s, that is, s is not handled at all
print "\n".join(["%s %s" %
(method.ljust(spacing),
processFunc(str(getattr(object, method).__doc__)))
for method in methodList])
#Here is a typical case of putting list parsing on several lines mentioned above. First, use method to iterate through the list of methodList s (a list of all callable properties of the objects obtained above), use processFunc to process u doc_u (docstring) of object.method, and use ljust-adjusted methodPrint the names together.And use'\n'. join to split each different method information into separate lines for printing.
5. Print information available for some functions
The following are the introspective methods used in the Python Self-Reflection Guide, which mainly use some built-in functions (id(),type(),repr(), and so on) to print information about objects:
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 way to use it as our introspective module, as follows:
>>> import apihelper # Load our apihelper >>> apihelper.info(apihelper) # Call the previous info function to see what attributes are in the apihelper info Print methods and doc strings. Takes module, class, list, dictionary, or string. interrogate Print useful information about item. >>> apihelper.interrogate(apihelper) # Use interrogate to view information about our apihelper NAME: apihelper CLASS: module ID: 3075144100 TYPE: <type 'module'> VALUE: <module 'apihelper' from 'apihelper.py'> CALLABLE: No Traceback (most recent call last): File "<stdin>", line 1, in <module> 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: info CLASS: function ID: 3075150932 TYPE: <type 'function'> VALUE: <function info at 0xb74b1454> CALLABLE: Yes DOC: Print methods and doc strings. >>> apihelper.interrogate(123) # View information about integers CLASS: int ID: 153002672 TYPE: <type 'int'> VALUE: 123 CALLABLE: No DOC: int(x[, base]) -> integer >>> apihelper.interrogate("Hello") # View string information CLASS: str ID: 3075198208 TYPE: <type 'str'> VALUE: 'Hello' CALLABLE: No DOC: str(object) -> string
At this point, our self-introspection module apihelper also has a more powerful function, its self-introspection ability is also good ~~~
=============================
Quote:
[1] http://www.ibm.com/developerworks/cn/linux/l-pyint/index.html?ca=drs-#ibm-pcon
[2] dive into python - section4: (Readable online)
http://woodpecker.org.cn/diveintopython/power_of_introspection/index.html
[3] http://www.cnblogs.com/huxi/archive/2011/01/02/1924317.html
Reprinted at: https://www.cnblogs.com/riasky/p/3507419.html