[Tutor] Transforming object into subclass instance

Jan Eden lists at janeden.org
Sun Oct 30 17:00:37 CET 2005


Hi,

my program uses an initial "switch" statement to create an object:

valid_types = dict(
    pages=Show.Page,
    syndicated=Show.Syndicated,
    authors=Show.Author,
    author_list=Show.AuthorList,
    galleries=Show.Gallery,
    pictures=Show.Picture,
    slideshow=Show.SlidePicture,
    tex=Show.Tex,
    blogs=Show.Blog,
    blogentries=Show.BlogEntry,
    search=Show.Search,
    search_author=Show.SearchAuthor,
    not_found=Show.NotFound
)
# ... code ommitted...

page = valid_types[type]]()

For a certain class, it is only during the execution of the __ini__ method that I can distinguish between two subtypes (subclasses). The subclasses define two different versions of method A.

Now my question is: How can I turn an object of class X into an object of either class Y or Z while (retaining all the attributes defined so far).

I know I could solve the problem by using another switch statement - but is there consistent OOP solution for this problem?

Thanks,

Jan
-- 
Hanlon's Razor: Never attribute to malice that which can be adequately explained by stupidity.


More information about the Tutor mailing list