Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52015
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Class hierarchy problem |
| Date | 2013-08-06 11:34 +0200 |
| Organization | None |
| References | <ktqeho$f5i$1@news.albasani.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.236.1375781680.1251.python-list@python.org> (permalink) |
BrJohan wrote:
> I'm in need of help to solve this Python (ver. 3.3) problem:
>
> I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA,
> SubC,...), each of which is inheriting from a chain of superclasses with
> a common baseclass(Sup) on top. (So far, no problem)
>
> 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)
>
> Now, the problem:
>
> How to design the __new__() and __init__() methods for the various
> classes in order to achieve what I want?
>
> (Some ten years I had the same problem (in a different context) and was
> helped by asking in this group. However, the solution has disappeared.
> Maybe the 2.x solution is not the same as in 3.x?)
Keep it simple, use a function:
def make(*args):
class_ = deduce_correct_class(*args)
return class_(*args)
That way you won't even need any __new__() methods.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Class hierarchy problem BrJohan <brjohan@gmail.com> - 2013-08-06 11:10 +0200
Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 10:30 +0100
Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 15:02 +0100
Re: Class hierarchy problem BrJohan <brjohan@gmail.com> - 2013-08-06 17:36 +0200
Re: Class hierarchy problem Joe Junior <joe.fbs.junior@gmail.com> - 2013-08-06 13:23 -0300
Re: Class hierarchy problem Jordi Riera <kender.jr@gmail.com> - 2013-08-06 18:13 +0200
Re: Class hierarchy problem Chris Angelico <rosuav@gmail.com> - 2013-08-06 18:12 +0100
Re: Class hierarchy problem Terry Reedy <tjreedy@udel.edu> - 2013-08-06 19:13 -0400
Re: Class hierarchy problem Ben Finney <ben+python@benfinney.id.au> - 2013-08-07 11:58 +1000
Re: Class hierarchy problem Peter Otten <__peter__@web.de> - 2013-08-06 11:34 +0200
Re: Class hierarchy problem Steven D'Aprano <steve@pearwood.info> - 2013-08-06 09:36 +0000
pexpect, loading an entry field inq1ltd <inq1ltd@inqvista.com> - 2013-08-06 11:05 -0400
Re: pexpect, loading an entry field "Lakshmipathi.G" <lakshmipathi.g@gmail.com> - 2013-08-06 22:04 +0530
Re: pexpect, loading an entry field inq1ltd <inq1ltd@inqvista.com> - 2013-08-06 14:03 -0400
Re: pexpect, loading an entry field "Lakshmipathi.G" <lakshmipathi.g@gmail.com> - 2013-08-07 13:40 +0530
csiph-web