Introspection and reflection mechanism of Python from the built-in properties and methods of Python objects

Tianyuan prodigal son 2020-11-13 09:06:20
introspection reflection mechanism python built-in

1. from dir() Function start

about dir() This Python Built in functions for ,Python The friends in the advanced group must be familiar with . I've introduced this function more than once . Whenever you want to know what properties and methods a class or class instance contains , I'm going to turn to this function .

>>> a = [3,4,5]
>>> type(a) # return a The type of , The result is list class 
<class 'list'>
>>> dir(a) # return list Class instance object a Contains properties and methods 
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> dir(list) # return list class a Contains properties and methods 
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

For modules 、 Built in functions , And custom classes ,dir() Make no exception , Still available .

>>> import math
>>> dir(math) # return math Module contains subitems ( Sub module 、 class 、 function 、 Constant etc. )
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> dir(max) # Returns the built-in child of a built-in function 
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']

Read here , There must be a lot of friends who will say , my PyCharm( It could be VSCode Or something ) And tell me , What properties and methods does the current object have , It's still automatically displayed , I don't need to do it . you 're right ,IDE It does provide us with a lot of convenience , however , Have you ever thought about it IDE How to realize these functions ? If your task is to design a similar IDE, You really don't want to understand Python Internal mechanism ?

2. Built in properties and methods

In the following code , class Player Two properties and one method are defined ,p yes Player An example of . call dir() The example shows that p Properties and methods of , You will find , Except for the definition in the code name,rating and say_hello() Outside , Everything else starts with a double underline 、 Ends with double underscores , These are the legends Python Object's built-in properties and methods .

>>> class Player:
""" Player class """
def __init__(self, name, rating=1800): = name
self.rating = rating
def say_hello(self):
""" Self reported name and grade """
print(' Hello everyone ! I'm a chess player %s, Current grade %d branch .'%(, self.rating))
>>> p = Player(' Tianyuan prodigal son ')
>>>> p.say_hello()
Hello everyone ! I'm a chess player , Current grade 1800 branch .
>>> for item in dir(p):

In these built-in properties and methods , Seems to be only __init__ and __new__ It looks a little familiar , What are the other uses for ? below , I'll show you a few of them .

2.1 _ doc _

__doc__ Is the most commonly used built-in property , There are a lot of partners who don't realize this . A standard code file , Except for the code itself , A lot of necessary information will be provided , Such as class 、 Function description , These instructions , We call it the document string (DocString).__doc__ Is the document string of the object .

>>> Player.__doc__
' Player class '
>>> p.__doc__
' Player class '
>>> p.say_hello.__doc__
' Self reported name and grade '

The document string shown here , I'm defining Player When you write a note in a specific location ( The little fellow who didn't notice that , Please go back to the front of Player Class definition code ).

2.2 _ module _

It's easy to guess , Built in properties __mudule__ Represents the module to which the object belongs . here ,Player Class and its instances , It's all current __main__ modular . If we introduce a module , It's easier to say __module__ The meaning of .

>>> Player.__module__
>>> p.__module__
>>> p.say_hello.__module__
>>> import math
>>> math.sin.__module__

2.3 _ dict _

Built in properties __dict__, Is a dictionary made up of key value pairs of attributes of an object . Class __dict__ And class instances __dict__ There are different performances .

>>> p.__dict__
'name': ' Tianyuan prodigal son ', 'rating': 1800}
>>> Player.__dict__
'__module__': '__main__', '__doc__': ' Player class ', '__init__': <function Player.__init__ at 0x000002578CF399D8>, 'say_hello': <function Player.say_hello at 0x000002578CF39A68>, '__dict__': <attribute '__dict__' of 'Player' objects>, '__weakref__': <attribute '__weakref__' of 'Player' objects>})

2.4 _ class _

By instantiating a class , You can get an instance of a class . So how to instance from a class , How about reverse getting classes ? actually , Built in properties of class instances __class__ It's class . We can use an example of __class__ To initialize another instance .

>>> pp = p.__class__(' Eight below zero ', 2100)
>>> pp.say_hello()
Hello everyone ! I'm a chess player , Current grade 2100 branch .

2.5 _ dir _

dir() The function is Python Built in functions for , Built in method __dir__ Be similar to dir() function ..

>>> p.__dir__()
['name', 'rating', '__module__', '__doc__', '__init__', 'say_hello', '__dict__', '__weakref__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']

2.6 _ getattribute _

seeing the name of a thing one thinks of its function ,__getattribute__ Returns the properties of an object —— It's actually an attribute or a method . This is a built-in approach , When used, it must be followed by parentheses , The parameter is the name of the specified property or method .

>>> p.__getattribute__('name')
' Tianyuan prodigal son '
>>> p.__getattribute__('rating')
>>> p.__getattribute__('say_hello')
<bound method Player.say_hello of <__main__.Player object at 0x000002578CF2CA88>>
>>> p.__getattribute__('say_hello')()
Hello everyone ! I'm a chess player , Current grade 1800 branch .

3. Dynamic loading and calling

The initial stage of learning any programming language , Almost all of us have a common problem : Create a variable or object dynamically . ad locum ,“ dynamic ” Just emphasize that variable or object names are not up to the programmer , It's up to other participants ( For example, the operator in an interactive program ,C/S or B/S The client in the program ) decision . Maybe it's not an accurate statement , But I can't think of a better word for this kind of application requirement .

With Python For example : Read a string from the keyboard , Create an integer object with the name of the string , Make it equal to 3. Usually , We use exec() Function can solve . Why not eval() Function? ?eval() The function simply evaluates an expression in the form of a string , Cannot complete assignment operation .

>>> var_name = input(' Please enter the integer object name :')
Please enter the integer object name :x
>>> exec('%s=3'%var_name)
>>> x

I understand “ dynamic ” The concept of , Let's look at how to load modules dynamically 、 How to call objects dynamically

3.1 Dynamic load module

according to Python Coding standards , Script files are generally imported into the module after encoding format declaration and document description . In some cases , The code needs to be based on the specific circumstances of the program runtime , Import the corresponding module temporarily —— Usually , In this case , The imported module name is specified by a string . The following code shows an example of a dynamic loading module .

>>> os.getcwd() # There is no import at this time os modular , So throw an exception 
Traceback (most recent call last):
File "<pyshell#158>", line 1, in <module>
NameError: name 'os' is not defined
>>> os = __import__('os') # Dynamic import 'os' modular 
>>> os.getcwd()

3.2 Get object by object name

This demand sounds a little strange , But there are a lot of people who come across .Player Class instance p For example , If we only have strings ’p’, How to get it p What's the example? ? We know that built-in functions globals() Returns the global object dictionary ,locals() Returns the object dictionary of the hierarchy , The key of these two dictionaries is the string of the object name . So here's the idea , It's easy to get an object by its name .

>>> obj = globals().get('p', None)
>>> obj
<__main__.Player object at 0x000002578CF2CA88>
>>> obj.say_hello()
Hello everyone ! I'm a chess player , Current grade 1800 branch .

3.3 Dynamic call object

The most typical application of dynamic call object is the implementation of service interface . If the client calls a service on the server by sending the name string of the service , The one-to-one correspondence between the name string and the service owner . If there is no dynamic call , I'm afraid the code has to be written like this .

if cmd == 'service_1':
elif cmd == 'service_2':
elif cmd == 'service_3':
... ...

The following code , It demonstrates how the server can dynamically call the corresponding service according to the received command .

>>> class ServiceDemo:
def service_1(self):
print('Run service_1...')
def service_2(self):
print('Run service_2...')
def onconnect(self, cmd):
if hasattr(self, cmd):
getattr(self, cmd)()
print(' Wrong command ')
>>> serv = ServiceDemo()
>>> serv.onconnect('service_1')
Run service_1...
>>> serv.onconnect('service_2')
Run service_2...
>>> serv.onconnect('hello')
Wrong command

4. Introspection and reflexive mechanisms

It's time to talk about introspection and reflection . however , As of now , I've finished with introspection and reflection , It's just that the words introspection and reflection are not used . Only from this point , It can be explained that , Introspection and reflection are totally redundant concepts . If you have a partner who doesn't understand these two concepts , It doesn't matter at all , It doesn't affect your understanding of programming at all .

So called introspection , The object itself provides the ability to view its own properties 、 Method 、 The means of type . Built in method __dir__ Isn't it the introspection of the object ? in addition , Built in functions dir()、type()、isinstance() Can provide some or all of the functions of introspection .

The reflection mechanism is Java and PHP A feature provided by languages such as , It's hard to describe exactly , In short , That is, you can get the properties and methods of objects in the running state , And call them at any time , The most typical application is to get the object through the string form object name . That's what I said “ Dynamic loading and calling ” Do you ?

Write here , I can't help but pay tribute to the foresight and sagacity of Uncle GUI : As early as Java Many years before it was born , Uncle GUI has already made a comprehensive plan Python Built in mechanisms for objects , It is far more forward-looking than the mechanism of introspection and reflection .

本文为[Tianyuan prodigal son]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database