极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

认识Python对象自省机制

极客小将2021-01-19-
简介相关免费学习推荐:python视频教程自省是通过一定的机制查询到对象的内部结构。Python中比较常见的自省(introspection)机制(函数用法)有:dir(),type(),hasattr(),isinstance(),通过这些函数,我们能够在程序运行时得知对象的类型,判断对象是否存在某个
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-b5506197d8.css"/>

qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

相关免费学习推荐:python视频教程qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

自省是通过一定的机制查询到对象的内部结构。qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

Python中比较常见的自省(introspection)机制(函数用法)有: dir(),type(), hasattr(),
isinstance(),通过这些函数,我们能够在程序运行时得知对象的类型,判断对象是否存在某个属性,访问对象的属性。qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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'>)

更多编程相关知识,请访问:编程教学!!

以上就是认识Python对象自省机制的详细内容,更多请关注少儿编程网其它相关文章!qgS少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

网友点评

共有5条评论来说两句吧...

在线客服