Create a subclass object with initialized parent object
Question: I have a BaseEntity class, which defines a bunch (a lot) of non-required
properties and has most of functionality. I extend this class in two others,
which have some extra methods, as well as initialize one required property.

    
    
    class BaseEntity(object):
    
        def __init__(self, request_url):
            self.clearAllFilters()
            super(BaseEntity, self).__init__(request_url=request_url)
    
        @property
        def filter1(self):
            return self.filter1
    
        @filter1.setter
        def filter1(self, some_value):
            self.filter1 = some_value
        ...
        def clearAllFilters(self):
            self.filter1 = None
            self.filter2 = None
            ...
    
        def someCommonAction1(self):
            ...
    
    class DefinedEntity1(BaseEntity):
    
        def __init__(self):
            super(BaseEntity, self).__init__(request_url="someUrl1")
    
        def foo():
            ...
    
    class DefinedEntity2(BaseEntity):
    
        def __init__(self):
             super(ConsensusSequenceApi, self).__init__(request_url="someUrl2")
    
        def bar(self):
            ...
    

What I would like is to initialize a BaseEntity object once, with all the
filters specified, and then use it to create each of the DefinedEntities, i.e.

    
    
    baseObject = BaseEntity(None)
    baseObject.filter1 = "boo"
    baseObject.filter2 = "moo"
    
    entity1 = baseObject.create(DefinedEntity1)
    

Looking for pythonic ideas, since I've just switched from statically typed
language and still trying to grasp the power of python.


Answer: One way to do it:

    
    
    import copy
    
    class A(object):
        def __init__(self, sth, blah):
            self.sth = sth
            self.blah = blah
    
        def do_sth(self):
            print(self.sth, self.blah)
    
    class B(A):
        def __init__(self, param):
            self.param = param
    
        def do_sth(self):
            print(self.param, self.sth, self.blah)
    
    a = A("one", "two")
    almost_b = copy.deepcopy(a)
    almost_b.__class__ = B
    B.__init__(almost_b, "three")
    almost_b.do_sth() # it would print "three one two"
    

Keep in mind that Python is an extremely open language with lot of dynamic
modification possibilities and it is better not to abuse them. From clean code
point of view I would use just a plain old call to superconstructor.

