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?
No matter it's a variable or a method, they are all considered "attributes". So hasattr returns True.
The way in which methods are different is that they are callable. This can be checked by calling callable. So if you want to figure out whether an attribute is callable, you can
if hasattr(obj, "attr1"):
if callable(obj.attr1):
# attr1 is a method!
else:
# attr1 is not a method but an attribute
else:
# attr1 is not an attribute
You can define hasmethod() as below
def hasmethod(obj,method_name):
return hasattr(obj,method_name) and callable(getattr(obj,method_name))
print(hasmethod(obj,'meth1')) #True
print(hasmethod(obj,'attr1')) #False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With