There is a base class Base and a subclass Special.
class Base(object):
def __init__(self, name):
self.name = name
def greet(self):
return 'Hello %s' % self.name
class Special(Base):
def __init__(self, name):
super(Special, self).__init__(name)
def rhyme(self):
return 'Hi %s! How are you? Fine, thanks. What about you?' % self.name
How can I turn an instance of Base into an instance Special? Currently I have a classmethod defined on Special that just reassignes __dict__:
class Special(Base):
...
@classmethod
def from_base(cls, baseobj):
special = Special()
special.__dict__ = baseobj.__dict__
return special
Is this idiomatic? If not what would be?
P.S. An example scenario: The base class is some default implementation. In the wild, you'll likely find objects of the base class. Now in some project, the base class has been subclasses and special methods have been added to the subclass. Now you mostly still work with base class objects, but from time to time you'll want to "upgrade" to the special class, because you'll need access to some methods.
Special? - Simeon Visser