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


Groups > comp.lang.python > #27114

Dynamically determine base classes on instantiation

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <thbach@students.uni-mainz.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; "'a'": 0.07; 'determines': 0.09; 'dict': 0.09; 'name):': 0.09; 'def': 0.10; 'cases': 0.15; "['a',": 0.16; 'foo(object):': 0.16; 'list)': 0.16; 'ugly.': 0.16; 'instance': 0.17; 'bit': 0.21; 'subject:skip:i 10': 0.22; 'defined': 0.22; 'seems': 0.23; 'pass': 0.25; 'header:User- Agent:1': 0.26; 'common': 0.26; 'topic': 0.27; 'assert': 0.29; 'obj': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'function': 0.30; 'instances': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'path': 0.35; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'two': 0.37; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'content-disposition:inline': 0.60; 'thomas': 0.62; 'provide': 0.62; 'received:85': 0.65; 'nice,': 0.84; 'received:10.94': 0.84; '\xe2\x80\xa6': 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=1345065531; x=1376601531; h=date:from:to:subject:message-id:mime-version: content-transfer-encoding; bh=hsRQ8ZMficIxoNo7u35+vZIvT8iGxMR/SXJpVzeGx90=; b=cPudi+rdGMhqzWrQnLZpSXXpthofOKU6sEfAJRn2yAESpwpgMamLOwOd 0EfIDx4YuUz7KYhAZyFIBd90tjkqjarT7nW+KBrQMoDtME6/ctFi2RvIV 44LqDsDx7wjk2mxjMXSCNdvz4IvlDPHMjxARZiKlCYUTSZDGf8BSoq2K1 w=;
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ap0EAEQQLFAKXgZQ/2dsb2JhbABFhgG1H4JKDwFeHQImAhYoCxYuh3aYV45Bky+BIY0iggoyYAOVTpATgmE
Date Wed, 15 Aug 2012 23:17:41 +0200
From Thomas Bach <thbach@students.uni-mainz.de>
To <python-list@python.org>
Subject Dynamically determine base classes on instantiation
MIME-Version 1.0
Content-Type text/plain; charset="utf-8"
Content-Disposition inline
Content-Transfer-Encoding 8bit
User-Agent Mutt/1.5.21 (2010-09-15)
X-Originating-IP [85.178.249.115]
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>
Newsgroups comp.lang.python
Message-ID <mailman.3324.1345065532.4697.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345065532 news.xs4all.nl 6959 [2001:888:2000:d::a6]:46364
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27114

Show key headers only | View raw


Hi list,

I'm confronted with a strang problem I cannot find a clean solution
for. To me it seems like I need meta-classes. Anyway, I stucked a bit
deeper in that topic and couldn't find a proper solution neither. But,
judge for yourselve.

I want a class that determines on instantiating its base classes
dynamically. Consider the following two use cases

a = Foo(['a', 'list'])  # returns an instance that behaves like a list
assert len(a) == 2
assert a[0] == 'a'
assert a == ['a', 'list']
assert isinstance(a, list) # This would be nice, but no must-have

b = Foo({'blah': 8}) # returns an instance that behaves like a dict
assert b['blah'] == 'blah'
assert b == {'blah': 8}
assert isinstance(b, dict) # again, no must-have

a.do_something()   # common function to both instances as defined
b.do_something()   # in the Foo class


What I'm currently doing something like the following:

class Foo(object):

    def __init__(self, obj):
        self._obj = obj

    def __len__(self):
        return len(self._obj)

    def __getitem__(self, name):
        return self._obj[name]

    # …

    def do_something(self):
        # do something on self._obj
        pass

Which seems ugly. Is there a way to provide the functions of `list'
and `dict' in Foo's look-up path without having to write all the
stubs myself?

Regards,
	Thomas Bach.

Back to comp.lang.python | Previous | NextNext 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