I am working with a Python class, and I don\'t have write access to its declaration. How can I attach a custom method (such as __str__) to the
__str__
>>> class C(object): ... pass ... >>> def spam(self): ... return 'spam' ... >>> C.__str__ = spam >>> print C() spam
It won't work on classes which use __slots__.