Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'elif': 0.04; 'dynamically': 0.07; 'raises': 0.07; 'so?': 0.07; 'type,': 0.07; 'python': 0.09; 'to:addr:comp.lang.python': 0.09; 'wraps': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'aug': 0.13; '__new__': 0.16; 'arg):': 0.16; 'argument.': 0.16; 'belongs': 0.16; 'cls': 0.16; 'foo(object):': 0.16; 'list):': 0.16; 'though)': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '(or': 0.18; 'together.': 0.21; '(on': 0.22; 'subject:skip:i 10': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'question': 0.27; '(as': 0.27; 'correct': 0.28; 'actual': 0.28; 'steven': 0.29; 'class': 0.29; 'classes': 0.30; 'returned': 0.30; 'thursday,': 0.30; 'function': 0.30; 'stuff': 0.30; 'could': 0.32; '+0200,': 0.33; 'function.': 0.33; '(with': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'so,': 0.35; 'received:209.85': 0.35; 'but': 0.36; "wasn't": 0.36; 'method': 0.36; 'should': 0.36; 'bad': 0.37; 'ok,': 0.37; 'why': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'think': 0.40; 'first': 0.61; 'thomas': 0.62; 'more': 0.63; 'august': 0.66; 'realized': 0.71; 'saw': 0.75; '"what': 0.84; 'buzz': 0.84; 'encounters': 0.84; 'tie': 0.84; '"how': 0.91 Newsgroups: comp.lang.python Date: Thu, 16 Aug 2012 10:03:51 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=92.251.100.26; posting-account=JAXb_goAAAB9-y8dD9qkq0SYCvESM0V_ References: <502c3bc2$0$29978$c3e8da3$5496439d@news.astraweb.com> <502d0d74$0$6986$e4fe514c@news2.news.xs4all.nl> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 92.251.100.26 MIME-Version: 1.0 Subject: Re: Dynamically determine base classes on instantiation From: Richard Thomas To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345136634 news.xs4all.nl 6930 [2001:888:2000:d::a6]:54876 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27182 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. On Thursday, 16 August 2012 18:54:12 UTC+2, Thomas Bach wrote: > On Thu, Aug 16, 2012 at 05:10:43PM +0200, Hans Mulder wrote: > > > On 16/08/12 14:52:30, Thomas Bach wrote: > > > > > > > > So, my question (as far as I can see it, please correct me if I am > > > > wrong) is less of the "How do I achieve this?"-kind, but more of the > > > > "What is a clean design for this?"-kind. My intuitive thought was that > > > > the `merge' function should be a part of the object returned from `F'. > > > > > > The misunderstanding is that you feel F should return an object with > > > a 'merge' method and a varying abse type, while Steven and others > > > think that F should be a function. > > > > OK, then my design wasn't so bad in the first place. :) > > > > I made a class `Model' which wraps the actual type and realized > > `merge' and `F' (with a better name, though) as classmethods of > > `Model' in order to tie together the stuff that belongs together. By > > the way, another need I saw for this design was that > > > > setattr(Model(), 'foo', {'bar': int}) > > > > works, whereas > > > > setattr(dict(), 'foo', {'bar': int}) > > > > raises an AttributeError (on Python 3.2). Could someone give me the > > buzz word (or even an explanation) on why that is so? > > > > Thomas Bach