hasattr The function is python Built in functions for , similar print The function is the same .
hasattr Function is used to determine whether the object contains the corresponding properties .
among builtins.py The script is about hasattr The function is described as follows :
def hasattr(*args, **kwargs): # real signature unknown
"""
Return whether the object has an attribute with the given name.
This is done by calling getattr(obj, name) and catching AttributeError.
"""
pass
hasattr(object, name)
among :
object – object .
name – Property name string .
Return value :
If the object has this property returned True, Otherwise return to False.
Check whether the image array has shape attribute .
# encoding: utf-8
import cv2
image=cv2.imread("./2.jpeg")
if hasattr(image,"shape"):
print("[INFO] The shape of the image :{0}".format(image.shape))
Run the following :