Suppose I have the following classes:
class Plain(object):
def speak(self):
print 'ho-hum'
class Fancy(Plain):
def exult(self):
print 'huzzah!'
plain = Plain()
plain.speak()
# ho-hum
plain.exult()
# ---------------------------------------------------------------------------
# AttributeError Traceback (most recent call last)
# <ipython-input-585-5f782c9ea88b> in <module>()
# ----> 1 plain.exult()
fancy = Fancy()
fancy.speak()
# ho-hum
fancy.exult()
# huzzah!
... and suppose that, for some reason, I want to "promote" an instance of Plain
so that it becomes an instance of Fancy
.
I know that I can always modify the instance's __class__
attribute:
plain.__class__ = Fancy
plain.exult()
# huzzah!
...but I gather from various SO posts (e.g., here, here) that this is not a good thing to do (at least not in production code).
Is there a more "production-code-worthy" way to promote an instance to a subclass?
FWIW, the real-world use-case that brings me to this problem is the following. I'm working with a 3rd-party library that implements a client for a web service. Among the things this client can do is return instances of various classes. These classes provide very few methods, and the few they provide are very weak. I can easily write subclasses for these classes with more, and more powerful, methods, but in order for them to be useful, I'd also need to write wrappers for those API functions that currently return instances of the API's original classes. Invariably, this wrappers would only have to promote the instances returned by the original ("wrappee") functions to instances of my enhanced subclasses.