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

Understand the Python object introspection mechanism

coldplay.xixi
Release: 2021-01-08 14:13:26
forward
2507 people have browsed it

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;>)
Copy after login
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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template