In the Python language, when some libraries are used, the documents found on the network are not complete, which requires checking whether the corresponding Python object contains the required function or constant. The following introduces how to check which attributes are contained in the Python object, such as member functions, variables, etc., where the Python object refers to objects with many elements such as classes, modules, and instances. Here, the OpenCV2 Python package cv2 is used as an example to explain.
Because OpenCV is implemented in C / C ++ language, all functions and variables are not packaged for Python users to call, and sometimes the corresponding documentation is not found on the network; there are also two versions of OpenCV: OpenCV2 and OpenCV3 This version also has some differences in the functions and variables used.
1. The dir () function
dir ( [ object ] ) will return a list of all valid attributes of object. Examples are as follows:
$ 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. The vars () function
vars ( [ object ] ) returns the __dict__ attribute of the object object, where the object object can be a module, class, instance, or any other object with the __dict__ attribute. Therefore, it is equivalent to directly accessing the __dict__ attribute. The example is as follows (here is a counterexample, there is no __dict__ attribute in the mser object):
>>> vars(mser) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: vars() argument must have __dict__ attribute >>> mser.__dict__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'cv2.MSER' object has no attribute '__dict__'
3. help () function
help ( [ object ] ) Call the built-in help system. Input
>>> help(mser)
The content is displayed as follows:
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__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
Press h key to display help information; press q key to exit.
4. type () function
type ( object ) returns the type of object object.
>>> type(mser) <type 'cv2.MSER'> >>> type(mser.detect) <type 'builtin_function_or_method'>
5. hasattr () function
hasattr ( object , name ) is used to determine whether name (string type) is an attribute of object, if it returns True, otherwise it returns False
>>> hasattr(mser, 'detect') True >>> hasattr(mser, 'compute') False
6. callable () function
callable ( object ): If the object object is callable, it returns True, otherwise it returns False. Note that the call may fail even if it returns True, but the call must fail if it returns False.
>>> callable(mser.detect) True
References
1. https://stackoverflow.com/questions/2675028/list-attributes-of-an-object
2. https://docs.python.org/2/library/functions.html