Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52015
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '(so': 0.07; 'problem:': 0.07; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; '...,': 0.16; 'hierarchy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sup': 0.16; 'wrote:': 0.18; '(in': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'subject:problem': 0.24; 'this:': 0.26; 'asking': 0.27; 'header:X -Complaints-To:1': 0.27; 'correct': 0.29; "i'm": 0.30; 'class': 0.32; 'maybe': 0.34; 'skip:d 20': 0.34; 'problem': 0.35; 'classes': 0.35; 'common': 0.35; 'except': 0.35; 'instances': 0.36; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'chain': 0.60; 'simple,': 0.60; 'solve': 0.60; 'decided': 0.64; 'different': 0.65; 'top.': 0.84; 'subject:Class': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Class hierarchy problem |
| Date | Tue, 06 Aug 2013 11:34:58 +0200 |
| Organization | None |
| References | <ktqeho$f5i$1@news.albasani.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508496ec.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.236.1375781680.1251.python-list@python.org> (permalink) |
| Lines | 32 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375781680 news.xs4all.nl 15914 [2001:888:2000:d::a6]:42293 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:52015 |
Show key headers only | View raw
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