Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27182

Re: Dynamically determine base classes on instantiation

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 <chardster@gmail.com>
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 <mailman.3382.1345136056.4697.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=92.251.100.26; posting-account=JAXb_goAAAB9-y8dD9qkq0SYCvESM0V_
References <mailman.3324.1345065532.4697.python-list@python.org> <502c3bc2$0$29978$c3e8da3$5496439d@news.astraweb.com> <mailman.3358.1345121602.4697.python-list@python.org> <502d0d74$0$6986$e4fe514c@news2.news.xs4all.nl> <mailman.3382.1345136056.4697.python-list@python.org>
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 <chardster@gmail.com>
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 <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>
Message-ID <mailman.3383.1345136634.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-15 23:17 +0200
  Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 00:16 +0000
    Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 14:52 +0200
      Re: Dynamically determine base classes on instantiation Hans Mulder <hansmu@xs4all.nl> - 2012-08-16 17:10 +0200
        Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 18:54 +0200
          Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:03 -0700
            Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:49 +0000
              Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-17 04:50 -0700
                Re: Dynamically determine base classes on instantiation Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-17 14:53 -0400
                Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-18 01:44 +0000
                Re: Dynamically determine base classes on instantiation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-18 11:36 +0100
          Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:03 -0700
            Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 19:27 +0200
      Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 18:04 +0000
    Re: Dynamically determine base classes on instantiation Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-16 12:29 -0400
    Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:09 -0700
      Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:45 +0000
    Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 19:18 +0200
      Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 18:08 +0000

csiph-web