It is actually possible without modifying the class based on this answer by Alex Martelli:
class A(object):
def __init__(self, words):
self.words = words
def get_words(self):
return self.words
a = A("I am a")
b = A("I am b")
def make_meth(inst, _cls, meth, lm):
inst.__class__ = type(_cls.__name__, (_cls,), {meth: lm})
make_meth(a, A, "__len__", lambda self: 12)
make_meth(b, A, "__len__", lambda self: 44)
print(len(b))
print(len(a))
print(a.get_words())
print(b.get_words())
If we run the code:
In [15]: a = A("I am a")
In [16]: b = A("I am b")
In [17]: make_meth(a, A, "__len__", lambda self: 12)
In [18]: make_meth(b, A, "__len__", lambda self: 44)
In [19]: print(len(b))
44
In [20]: print(len(a))
12
In [21]: print(a.get_words())
I am a
In [22]: print(b.get_words())
I an b
As per the last part of the last part of the linked answer, you can add any methods on a per instance basis using inst.specialmethod
once you have used inst.__class__ = type(...
:
In [34]: b.__class__.__str__ = lambda self: "In b"
In [35]: print(str(a))
<__main__.A object at 0x7f37390ae128>
In [36]: print(str(b))
In b
__len__
, try defining it as an actual method on the class instead of an attribute of an instance. – Dine__len__
methods for different instances ofA
. – GuntarA
object, or you should inherit fromA
. I recommend the wrapper; inheritance is deceptively tricky, especially when the superclass isn't designed for it. – Bolterinst.__len__
? – Rusk