View properties of Python objects

https://www.cnblogs.com/klchang/p/7296058.html

https://www.cnblogs.com/cenyu/p/5713686.html

https://www.cnblogs.com/elie/p/6685413.html

 

In Python, when some libraries are used, the documents found on the network are not complete, so it is necessary to check whether the corresponding Python objects contain the required functions or constants. Here's how to see which attributes are included in Python objects, such as member functions and variables. Python objects here refer to objects with more elements, such as classes, modules and instances. Here we take OpenCV2's Python package cv2 as an example to illustrate.
Because opencv is implemented in C/C + + language, it does not package all functions and variables for Python users to call, and sometimes the corresponding documents cannot be found on the network; there are also two versions of OpenCV: OpenCV2 and OpenCV3, which also have some differences in the functions and variables used.

1. dir() function

Direct ([object]) returns a list of all valid properties of the object. An example is 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. vars() function

vars([object]) returns the__ dict__ Property, where the object object object can be a module, class, instance, or any other__ dict__ Property. So, it's associated with direct access__ dict__ Property equivalence. The example is as follows (in this case, there is no__ dict__ Properties):

>>> 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]) calls the built-in help system. input

>>> help(mser)

The display contents are 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 to display the help message; press q to exit.

4. type() function

type(object) returns the type of the 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 the object object. If it returns True, otherwise it returns False

>>> hasattr(mser, 'detect')
True
>>> hasattr(mser, 'compute')
False

6. callable() function

callable(object): returns True if the object object is callable, otherwise False. Note that even if you return True, the call may fail, but the return False call must fail.

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

 

============== End

Tags: Python Attribute network OpenCV

Posted on Fri, 29 May 2020 05:59:59 -0400 by angelorigo