Python 的自省是什么?-Python教程

资源魔 23 0

甚么是自省?

正在一样平常生存中,自省(introspection)是一种自我反省行为。

正在较量争论机编程中,自省是指这类才能:反省某些事物以确定它是甚么、它晓得甚么和它能做甚么。自省向顺序员提供了极年夜的灵敏性以及管制力。

说的更简略直白一点:自省就是面向工具的言语所写的顺序正在运转时,可以晓得工具的类型。简略一句就是,运转时可以获知工具的类型。

例如python, buby, object-C, c++都有自省的才能,这外面的c++的自省的才能最弱,只可以晓得是甚么类型,而像python能够晓得是甚么类型,另有甚么属性。

最佳的了解自省就是经过例子: Type introspection 这里是各类编程言语中自省(introspection)的例子(这个链接里的例子很首要,兴许你很难经过叙说了解甚么是introspection,然而经过这些例子,一会儿你就能够了解了)

回到Python,Python中比拟常见的自省(introspection)机制(函数用法)有: dir(),type(), hasattr(), isinstance(),经过这些函数,咱们可以正在顺序运转时患上知工具的类型,判别工具能否存正在某个属性,拜访工具的属性。

dir()

dir() 函数多是 Python 自省机制中最驰名的局部了。它前往通报给它的任何工具的属性称号通过排序的列表。假如没有指定工具,则 dir() 前往以后作用域中的称号。让咱们将 dir() 函数使用于 keyword 模块,并察看它揭示了甚么:


>>> import keyword
>>> dir(keyword)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main']

type()

type() 函数有助于咱们确定工具是字符串仍是整数,或是其它类型的工具。它经过前往类型工具来做到这一点,能够将这个类型工具与 types 模块中界说的类型相比拟:


>>> type(42)<class 'int'>
>>> type([])<class 'list'>

isinstance()

能够应用 isinstance() 函数测试工具,以确定它能否是某个特定类型或定制类的实例:


>>> isinstance("python", str)
True

Python自省中help用法扩大:

关上python的IDLE,就进入到了python诠释器中,python诠释器自身是被以为是一个主模块,而后正在诠释器提醒符>>>下输出一些咱们想理解的信息,以是起首咱们会先寻求协助,以是输出help,接着输出help(),咱们就进入了help utility,而后循着提醒keywords,modules,以理解python的要害字和python自带的或许咱们额定装置以及界说的模块,假如要加入,输出'q',而后回车。

假如咱们想理解某个工具(python外面一切工具均可以以为是工具),也能够乞助也help(),不外要正在括号里输出工具的称号,格局help(object),例如help(print),鉴于工具的自省内容太多,有的只粘贴出局部内容。


>>> 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         import       return
True        elif        in         try
and         else        is         while
as         except       lambda       with
assert       finally       nonlocal      yield
break        for         not         
class        from        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 su妹妹ary contain the string "spam".
>>> help('print')
Help on built-in function 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.

保举教程:《Python教程》《Python教程》

以上就是Python 的自省是甚么?的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题 自省

抱歉,评论功能暂时关闭!