473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

__getitem__ on new-style classes

What i'm trying to do is tie special methods of a "proxy" instance
to another instance:

def test1():
class Container:
def __init__( self, data ):
self.data = data
self.__getitem__ = self.data.__getitem__

data = range(10)
c = Container(data)
print "test1"
print c[3]

This works OK. But when I try the same thing on
new style classes:

def test2():
print "test2"
class Container(object):
def __init__( self, data ):
self.data = data
# self.__dict__["__getitem__"] = self.data.__getitem__
self.__setattr__( "__getitem__", self.data.__getitem__ )
data = range(10)
c = Container(data)
print
print c.__getitem__(3) # works OK
print c[3] # fails

I get a "TypeError: unindexable object". It
seems that c[] does not call __getitem__ in this case.

The plot thickens, however, when one tries the following:

def test3():
data = range(10)
c = type( "Container", (), { "__getitem__":data.__getitem__ } )()
print "test3"
print c[3]

Which works fine. However, if I need to resort to
such trickery, no-one here where i work will have any idea
what it does :)

Would anyone like to shed some light on what is going on here ?

bye,

Simon.

Jul 18 '05 #1
1 1969
si***@arrowtheory.com wrote:
What i'm trying to do is tie special methods of a "proxy" instance
to another instance: .... new style classes:

def test2():
print "test2"
class Container(object):
def __init__( self, data ):
self.data = data
# self.__dict__["__getitem__"] = self.data.__getitem__
self.__setattr__( "__getitem__", self.data.__getitem__ )
data = range(10)
c = Container(data)
print
print c.__getitem__(3) # works OK
print c[3] # fails

I get a "TypeError: unindexable object". It
seems that c[] does not call __getitem__ in this case. because __getitem__ is looked up in the class, not the instance dictionary
The plot thickens, however, when one tries the following:

def test3():
data = range(10)
c = type( "Container", (), { "__getitem__":data.__getitem__ } )()
print "test3"
print c[3]


Here you've created an entry in the class dictionary, just like writing
data = range(10)
class Container(object):
__getitem__ = data.__getitem__

but, this probably doesn't help you much if you want the method to delegate to
an attribute of the instance, since the instance is unavailable at class
definition time

So, one solution, is to use create a delegating descriptor, like so:

class Delegate(object):
def __init__(self, attrname):
self.attrname = attrname
def __get__(self, obj, cls):
if isinstance(obj, cls):
# Note that the attribute is looked up on obj._data
return getattr(obj._data, self.attrname)
else:
return self

class Example(object):
__getitem__ = Delegate("__getitem__")

def __init__(self, delegate):
self._data = delegate
e = Example(range(10))
e[3]

3
Michael
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KanZen | last post by:
I'm trying to understand the difference between __setitem__ and an ordinary method. For example: >>> class A(object): def __getitem__(self, *args): print len(args) def normalMethod(self,...
0
by: Karl Chen | last post by:
Hi. Is it possible to make a classmethod out of __getitem__? I.e. I want to do: class C: def __getitem__(p): return 1+p
7
by: Steven Bethard | last post by:
So, GvR said a few times that he would like to get rid of lambda in Python 3000. Not to start up that war again, but I'm trying to eliminate unnecessary lambdas from my code, and I ran into a case...
0
by: It's me | last post by:
I've built a Python application using PythonCard 1.9 and Python 2.3 running under Windows XP. Everything works except that when I use the keyboard instead of the mouse to do certain operations in...
3
by: Tobiah | last post by:
#!/usr/bin/python # Hi, # # I noticed something interesting when trying to define # the __getitem__() method in a class that inherits from # (dict). If within the __getitem__ method I attempt...
7
by: David Isaac | last post by:
I have a subclass of dict where __getitem__ returns None rather than raising KeyError for missing keys. (The why of that is not important for this question.) I was delighted to find that...
2
by: Giovanni Bajo | last post by:
Hello, given the following object: .... def __getitem__(self, idx): .... if idx >= 10: raise IndexError .... return idx .... def __len__(self): .... ...
0
by: dackz | last post by:
>>class ListyThing(list): pass .... Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError <type 'list'> I don't find this intuitive. Is this intentional? I...
3
by: tsm8015 | last post by:
I do not think I am understanding how to redefine the getitem function for string. Why does the following not work: class NStr(str): def __getitem__(self,idx): print...
6
by: Andreas Matthias | last post by:
The following code doesn't run but I hope you get what I am trying to do. class my_dict (dict): def __getitem__ (self, key, crazy = False): if crazy == True: return 5 * self.get(key) else:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.