Class hierarchy problem

Chris Angelico rosuav at gmail.com
Tue Aug 6 10:02:23 EDT 2013


On Tue, Aug 6, 2013 at 2:58 PM, BrJohan <brjohan at gmail.com> wrote:
> On 06/08/2013 11:30, Chris Angelico wrote:
>>
>> On Tue, Aug 6, 2013 at 10:10 AM, BrJohan <brjohan at gmail.com> wrote:
>>>
>>> Now, I want to create instances of the correct subclasstype as decided by
>>> the common baseclass, like this:
>>>
>>> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)
>>>
>>> where i can be of any class except Sup itself (as decided by Sup)
>>
>>
>> Can you do this as a factory function instead of the class itself?
>> Then all you need to do is call the appropriate class.
>>
>> ChrisA
>>
>
> My classhierarchy is like a multilevel tree where each non-leaf node (class)
> is given knowledge about its nearest subclasses and their 'capacities'.
>
> So, my idea is to let the 'upper' class recursively choose which of its
> nearest subclasses is the 'correct' one, until approaching a 'leaf' class
> from which the instance should be created. And, given my knowledge that a
> solution along the lines of this idea has been designed and was working, I'm
> still hopeful ... (or I'll have to investigate all those old backup-DVDs)

[ responding on-list - I hope it was mere oversight that had this come
privately to me alone ]

This is code smell; this recursive search for the "right" class seems
likely to be wrong. Can you have the classes perhaps register
themselves in some way? On what basis is a superclass to determine
that one of its subclasses should handle this request?

ChrisA



More information about the Python-list mailing list