Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; 'elif': 0.04; 'dynamically': 0.07; 'def': 0.10; 'aug': 0.13; '__new__': 0.16; 'arg):': 0.16; 'argument.': 0.16; 'cls': 0.16; 'foo(object):': 0.16; 'list):': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'working.': 0.17; 'subject:skip:i 10': 0.22; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'mind.': 0.27; '-0700,': 0.29; 'class': 0.29; 'classes': 0.30; 'could': 0.32; 'to:addr :python-list': 0.33; 'thanks': 0.34; 'richard': 0.35; 'similar': 0.35; 'something': 0.35; 'but': 0.36; 'charset:us-ascii': 0.36; 'subject:: ': 0.38; 'behind': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'content- disposition:inline': 0.60; 'thomas': 0.62; 'encounters': 0.84; 'mechanics': 0.84; 'received:10.94': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1345138122; x=1376674122; h=date:from:to:subject:message-id:references:mime-version: in-reply-to; bh=7oZVAIvRyHuFqHP2RRwLA2V1pmWEfTxtczoMfczB+ec=; b=XFGAxBkLrxh+eH5xoZZnRIpXJ2+2MwtPIk8ZbNGzLlhJy/PxPqM9NKou DkvGMiSG1bjre24EYw0wVKaVE00nxiyQ8Z7zi8oE7p2VY6IslrAFeSiMI SkbCRkpoBVIk9xfseUwbdiVddYXvKM2MpyNyRJreLqET2mEUAv/WbFjlI k=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwEACotLVAKXgZQ/2dsb2JhbABFuzSCIAEBBTpPCAMYHhAUKCEuh3a6aosKgzuCPGADlU6QE4Jh Date: Thu, 16 Aug 2012 19:27:34 +0200 From: Thomas Bach To: Subject: Re: Dynamically determine base classes on instantiation References: <502c3bc2$0$29978$c3e8da3$5496439d@news.astraweb.com> <502d0d74$0$6986$e4fe514c@news2.news.xs4all.nl> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: [92.225.137.41] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345138122 news.xs4all.nl 6924 [2001:888:2000:d::a6]:39477 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27186 On Thu, Aug 16, 2012 at 10:03:51AM -0700, Richard Thomas wrote: > class Foo(object): > def __new__(cls, arg): > if isinstance(arg, list): > cls = FooList > elif isinstance(arg, dict): > cls = FooDict > return object.__new__(cls, arg) > > class FooList(Foo, list): > pass > > class FooDict(Foo, dict): > pass > > You could even have __new__ make these Foo* classes dynamically when > it encounters a new type of argument. > > Chard. Thanks for that one. Your solution just hit me like a punch in the face. I had something similar in my mind. But I could not work out how the mechanics behind it are working. Regards, Thomas