Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27114
| Date | 2012-08-15 23:17 +0200 |
|---|---|
| From | Thomas Bach <thbach@students.uni-mainz.de> |
| Subject | Dynamically determine base classes on instantiation |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3324.1345065532.4697.python-list@python.org> (permalink) |
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 | Next — Next in thread | Find similar | Unroll 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