Dynamically replacing an objects __class__; is it safe?

marco.nawijn at colosso.nl marco.nawijn at colosso.nl
Wed Mar 15 05:54:49 EDT 2017


Dear All,

Summary of the question:
Is it generally safe to dynamically change an objects class; if not 
under which conditions can it be considered safe.

Context:
Given the code below, I have no direct control over Base and M1. M1
is a instantiated by 'calling' the read-only property of Base. 
I would like to transparently switch behaviour from M1 to M2. I have
tried several things, but finally settled on the code below. Although
it works really well, I am not sure whether I am about to shoot myself 
in the foot. In short, what I do is derive from Base, shadow the read-only
property 'my_prop' so it returns M2 and finally replace an objects __class__
attribute with my derived class.

Any thoughts or comments?

(code below is Python 3)

class M1(object):

    def __init__(self):
        print('Initializing M1')


class M2(object):
                                                                                     
    def __init__(self):                                                              
        print('Initializing M2')                                                     
                                                                                     
                                                                                     
class Base(object):                                                                  
                                                                                     
    @property                                                                        
    def my_prop(self):                                                               
        return M1()


class ShadowBase(Base):

    @property
    def my_prop(self):
        return M2()

if __name__ == '__main__':

    o = Base()
    o.my_prop
    # Prints 'Initializing M1'

    o = Base()
    o.__class__ = ShadowBase
    o.my_prop
    # Prints 'Initializing M2'


More information about the Python-list mailing list