I want to dynamically implement __str__
method on a object if the object doesn't already implement it.
I try using hasattr(obj, '__str__')
it always returns me true as it picks it up from object class.
Is there a way to determine if an object actually implements __str__
?
I know I can use inspect.getmembers(obj)
but I am searching for a more pythonic way
EDIT
class Employee(object):
def __init__(self, name, age, emp_code):
self.name = name
self.age = age
self.emp_code = emp_code
Test
e = Employee("A", 23, "E1")
print hasattr(e, '__str__')
>> True
I want a check that returns False
instead of picking up the method inherited from object
.