Instantiating sub-class from super
On 2019-10-14 10:55 PM, DL Neil via Python-list wrote:
> Is there a technique or pattern for taking a (partially-) populated
> instance of a class, and re-creating it as an instance of one of its
> In a medically-oriented situation, we have a Person() class, and start
> collecting information within an instance (person = Person(), etc).
> During the data-collection process the person's sex may become obvious,
> eg few males have become/been pregnant.
> We could stick with Person() and implement specific methods therein,
> rather than separate Man and Woman sub-classes, but...
> It seemed better (at the design-level) to have Man( Person ) and Woman(
> Person ) sub-classes to contain the pertinent attributes, source more
> detailed and specific questions, and collect such data; by gender.
> In coding-practice, once gender becomes apparent, how should the
> instance of the Man() or Woman() sub-class be created - and established
> with the ID and other attributes previously collected as a Person instance?
> This attempt seems hack-y:
> ????man = Man()
> ????man.__dict__.update( person.__dict__ )
> Is there a pythonic tool for such, or is the task outlined
Here is a link to an article entitled 'Understanding Hidden Subtypes'.
It dates back to 2004, but I think it is still relevant. It addresses
precisely the issues that you raise, but from a data-modelling
perspective, not a programming one.
I found it invaluable, and applied the concepts in my own
business/accounting application. Having created the ability to make
subtypes visible and explicit, I found all kinds of unexpected uses for
The article seems to be missing a couple of images (Figure 1 and Figure
2) showing the data relationships. I downloaded the original article
onto my computer years ago, and my local copy does have the images, so
if you would like to see them let me know and I will upload my version
somewhere to make it accessible.