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


Groups > comp.lang.python > #27182

Re: Dynamically determine base classes on instantiation

Newsgroups comp.lang.python
Date 2012-08-16 10:03 -0700
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>
Subject Re: Dynamically determine base classes on instantiation
From Richard Thomas <chardster@gmail.com>
Message-ID <mailman.3383.1345136634.4697.python-list@python.org> (permalink)

Show all headers | 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