当前位置: 首页 > 图灵资讯 > 行业资讯> python如何查看对象的属性

python如何查看对象的属性

来源:图灵python
时间: 2026-09-23 14:49:21

在Python语言中,一些图书馆在使用时在互联网上发现的文档并不完整,因此需要检查相应的Python对象是否包含所需的函数或常量。以下介绍

让我们来看看Python对象中包含哪些属性,如成员函数、变量等。这里的Python对象指的是类别、模块、实例等元素。

对象。以OpenCV2Python包CV2为例,在此说明。

1. dir() 函数

dir([object]) 所有有效属性列表将返回object。示例如下:

$python
Python2.7.8(default,Sep242015,18:26:19)
[GCC4.9.20150212.9.2-6]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.
>>>importcv2
>>>mser=cv2.MSER()
>>>dir(mser)
['__class__','__delattr__','__doc__','__format__','__getattribute__','__hash__','__init__',
'__new__','_
_reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','detect',
'empty','getAlgorithm','getBool','getDouble','getInt','getMat','getMatVector','getParams','getString',
'paramHelp','paramType','setAlgorithm','setBool','setDouble','setInt','setMat','setMatVector',
'setString']

2. vars() 函数

vars([object]) 回到object对象__dict__属性,object对象可以是模块,类别,例子,或者其他任何__dict__属性的对象。因此,它与

直接访问__直接访问dict__属性等价。示例如下(这里是反例,mser对象中没有__dict__属性):

>>>vars(mser)
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<module>
TypeError:vars()argumentmusthave__dict__attribute
>>>mser.__dict__
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<module>
AttributeError:'cv2.MSER'objecthasnoattribute'__dict__'

3. help() 函数

help([object])调用内置帮助系统。输入

>>>help(mser)

显示内容如下:

HelponMSERobject:

classMSER(FeatureDetector)
|Methodresolutionorder:
|MSER
|Featuredetector
|Algorithmm
|__builtin__.object
|
|Methodsdefinedher:
|
|__repr__(...)
|x.__repr__()<==>repr(x)
|
|detect(...)
|detect(image[,mask])->msers
|
|----------------------------------------------------------------------
|Datandotherattributesdefineredher:
|
|__new__=<built-inmethod__new__oftypeobject>
|T.__new__(S,...)->anewobjectwithtypeS,asubtypeofT

更多技术请关注Python视频教程。