新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何查看對象的屬性
在Python語言中,有些庫在使用時(shí),在網(wǎng)絡(luò)上找到的文檔不全,這就需要查看相應(yīng)的Python對象是否包含需要的函數(shù)或常量。下面介紹

一下,如何查看Python對象中包含哪些屬性,如成員函數(shù)、變量等,其中這里的Python對象指的是類、模塊、實(shí)例等包含元素比較多的
對象。這里以O(shè)penCV2的Python包c(diǎn)v2為例,進(jìn)行說明。
1. dir() 函數(shù)
dir([object]) 會返回object所有有效的屬性列表。示例如下:
$ python Python 2.7.8 (default, Sep 24 2015, 18:26:19) [GCC 4.9.2 20150212 (Red Hat 4.9.2-6)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> 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() 函數(shù)
vars([object]) 返回object對象的__dict__屬性,其中object對象可以是模塊,類,實(shí)例,或任何其他有__dict__屬性的對象。所以,其與
直接訪問__dict__屬性等價(jià)。示例如下(這里是反例,mser對象中沒有__dict__屬性):
>>> vars(mser) Traceback (most recent call last): File "", line 1, in TypeError: vars() argument must have __dict__ attribute >>> mser.__dict__ Traceback (most recent call last): File " ", line 1, in AttributeError: 'cv2.MSER' object has no attribute '__dict__'
3. help() 函數(shù)
help([object])調(diào)用內(nèi)置幫助系統(tǒng)。輸入
>>> help(mser)
顯示內(nèi)容,如下所示:
Help on MSER object: class MSER(FeatureDetector) | Method resolution order: | MSER | FeatureDetector | Algorithm | __builtin__.object | | Methods defined here: | | __repr__(...) | x.__repr__() <==> repr(x) | | detect(...) | detect(image[, mask]) -> msers | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ =| T.__new__(S, ...) -> a new object with type S, a subtype of T
更多技術(shù)請關(guān)注Python視頻教程。
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:python如何查看對象的屬性
當(dāng)前網(wǎng)址:http://www.5511xx.com/article/djpggid.html


咨詢
建站咨詢
