As some good people showed me that callable()
can be used to solve this problem, I still found it to be a different question, because anyone that has this question in mind will not found the answer because he won't connect this directly to callable()
. Plus I found a possible way to go around without using callable()
, which is to use type()
as showed in one of the answers from myself.
Assume I create a simple class as Cls
class Cls():
attr1 = 'attr1'
def __init__(self, attr2):
self.attr2 = attr2
def meth1(self, num):
return num**2
obj = Cls('attribute2')
print(hasattr(obj, 'attr1')) # >>> True
print(hasattr(obj, 'attr2')) # >>> True
print(hasattr(obj, 'meth1')) # >>> True
From what I learned, an attribute is a variable inside a class, and a method is a function inside a class. They are not the same.
Apparently, there is no hasmethod()
to be called by python. And it seems that hasattr()
really gives all True
to my test on 'attr1'
, 'attr2'
, 'meth1'
. It does not differentiate from an attribute or a method.
And if I use dir()
, the attributes and methods will all show up in the output, and you can't really tell which one is what type either.
Can someone please explain me why?