I want to dynamically implement the __str__ method on an object if the object has not yet implemented it.
I try to use hasattr(obj, '__str__') , it always returns true to me, as it takes it from the class of the object.
Is there a way to determine if an object really implements __str__ ?
I know I can use inspect.getmembers(obj) , but I'm looking 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 choosing a method inherited from object .
python
Bhushan
source share