What is the best (or 'Pythonic') way to test if a class has a specific method defined?
Both of these work but don't feel 'correct' in that in the second one, I just try to access it and trap for an exception if it doesn't exist.
Is there a better / more correct way?
class TestClass(object):
def TestFunc(self):
pass
if 'TestFunc' in dir(TestClass):
print 'yes'
else:
print 'No'
try:
if TestClass.__getattribute__(TestClass, 'TestFunc'):
print 'yes'
except:
print 'No'