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


Groups > comp.lang.python > #27115

Re: Dynamically determine base classes on instantiation

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-1.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*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; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'function?': 0.16; 'list)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'ugly.': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'bit': 0.21; 'subject:skip:i 10': 0.22; 'defined': 0.22; 'seems': 0.23; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'topic': 0.27; 'assert': 0.29; 'obj': 0.29; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'function': 0.30; 'received:84': 0.32; '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; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'list,': 0.39; 'received:192.168': 0.40; 'thomas': 0.62; 'provide': 0.62; 'header :Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'nice,': 0.84; 'reply-to:addr:python.org': 0.84; '\xe2\x80\xa6': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=IekFqBWa c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=uU90gwi264UA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=C32CH4lRBacA:10 a=7icXDx-Utaj1t2iG268A:9 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Wed, 15 Aug 2012 22:36:11 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Dynamically determine base classes on instantiation
References <20120815211740.GA5570@roku>
In-Reply-To <20120815211740.GA5570@roku>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.3326.1345066753.4697.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345066753 news.xs4all.nl 6918 [2001:888:2000:d::a6]:52771
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27115

Show key headers only | View raw


On 15/08/2012 22:17, Thomas Bach wrote:
> 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?
>
Does Foo have to be a class? Couldn't it just be a factory function?

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


Thread

Re: Dynamically determine base classes on instantiation MRAB <python@mrabarnett.plus.com> - 2012-08-15 22:36 +0100

csiph-web