Home > Article > Backend Development > 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(),For more programming-related knowledge, please visit: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 '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
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!