git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Instantiating sub-class from super


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 
sub-classes?


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 
fundamentally-inappropriate?

-- 
Regards,
=dn