**What is introspection? **
In daily life, 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 programmers with great flexibility and control.
To put it more simply and bluntly: introspection is the ability to know the type of object when a program written in an object-oriented language is running. In a nutshell, the type of object can be known at runtime.
For example, python, buby, object-C, and c++ all have the ability to introspect. The introspective ability of C++ is the weakest. It can only know what type it is, while python can know what type it is and what attributes it has.
The best way to understand introspection is through examples: Type introspection Here are examples of introspection in various programming languages (the examples in this link are very important. It may be difficult for you to understand what introspection is through narration, but through these examples, You can understand)
Back to Python, the more common introspection mechanisms (function usage) in Python are: dir(), type(), hasattr(), isinstance(). Through these functions, we can know the object's status while the program is running. Type, to determine whether an object has a certain property, and to access the object's properties.
dir()
The dir() function is probably the most famous part of Python's introspection mechanism. It returns a sorted list of the attribute names of any objects passed to it. If no object is specified, dir() returns the name in the current scope. Let's apply the dir() function to the keyword module and observe what it reveals:
import keyword
dir(keyword)['__all__','__builtins__','__doc__','__file__','__name__','__package__','iskeyword','kwlist','main']
type()
The type() function helps us determine whether the object is a string or an integer, or other types of objects. It does this by returning a type object, which can be compared with the types defined in the types module:
type(42)<class'int'type([])<class'list'
**isinstance() **
You can use the isinstance() function to test an object to determine whether it is an instance of a specific type or custom class:
isinstance("python", str)
True
Extension of help usage in python introspection:
Open the IDLE of python, and enter the python interpreter. The python interpreter itself is considered a main module. Then enter some information we want to know at the interpreter prompt, so first we will ask for help first, so enter help, then enter help(), we enter the help utility, and then follow the prompt keywords and modules to understand the keywords of python and the modules that python comes with or we install and define additionally. If you want to exit, enter'q ', and then press Enter.
If we want to understand an object (all objects in python can be regarded as objects), we can also ask for help and help(), but we must enter the name of the object in parentheses in the format help(object), such as help(print), given that The subject's introspection content is too much, and some only paste part of the content.
help
Type help()for interactive help, or help(object)for help about object.help()
Welcome to Python 3.6's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.6/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"....
help keywords
Here is a list of the Python keywords. Enter any keyword to get more help.
False def if raise
None del importreturn
True elif intry
and else is whileas except lambda with
assert finally nonlocal yieldbreakfor not
classfrom or
continue global pass
help modules
Please wait a moment while I gather a list of all available modules...
PIL base64 idlelib runpy
__ future__ bdb idna runscript
__ main__ binascii idna_ssl sched
_ ast binhex imaplib scrolledlist
_ asyncio bisect imghdr search
_ bisect browser imp
...
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".help('print')
Help on built-infunction print in module builtins:print(...)print(value,..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object(stream); defaults to the current sys.stdout.
sep: string inserted between values,default a space.
end: string appended after the last value,default a newline.
flush: whether to forcibly flush the stream.
So far this article on what is python introspection is introduced. For more related python introspection, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!
Recommended Posts