Home  >  Article  >  Backend Development  >  Understand the Python object introspection mechanism

Understand the Python object introspection mechanism

coldplay.xixi
coldplay.xixiforward
2021-01-08 10:19:122486browse

Understand the Python object introspection mechanism

#Related free learning recommendations: python video tutorial

Introspection is to query the internal structure of the object through a certain mechanism.

The more common introspection mechanisms (function usage) in Python are: dir(), type(), hasattr(),

isinstance(). Through these functions, we can run the program When you know the type of the object, determine whether a certain attribute exists in the object, and access the attributes of the object.

class A(object):
    def __init__(self):
        print("A")


class C(A):
    def __init__(self):
        print("B")
        super().__init__()


class B(A):
    def __init__(self):
        print("C")
        super().__init__()


class D(B,C):
    def __init__(self):
        print("D")
        super().__init__()


if __name__ == '__main__':
    d = D()

运行结果:
D
C
B
A
(<class &#39;__main__.D&#39;>, <class &#39;__main__.B&#39;>, <class &#39;__main__.C&#39;>, <class &#39;__main__.A&#39;>, <class &#39;object&#39;>)
For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of Understand the Python object introspection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete