#Use the hasattr() Function

print(hasattr(list, 'sort'))
print(hasattr("pynerds", "capitalize"))

print(hasattr((1, 2), 'sort'))

The hasattr() function checks whether an object has an attribute with the given name. An attribute can be a  variable, method, property, data member, etc.

hasattr(obj, attr)
obj The object of which the attribute has to be checked
attr A string specifying the name of the the attribute whose presence has to be tested in obj.

The function returns True if the object contains the specified attribute name, otherwise False.


hasattr(str, 'join')
hasattr(str, 'lower')
hasattr(str, 'isalpha')
hasattr(str, 'sort')
L = []
hasattr(L, 'sort')
hasattr(L, '__len__')
hasattr(L, 'append')
S = set()
hasattr(S, 'add')
hasattr(S, 'update')

Example with user defined object

class Demo:
    x = 10 
    def demofunc():

print(hasattr(Demo, 'x'))
print(hasattr(Demo, 'demofunc'))
print(hasattr(Demo, 'y'))