[IronPython] in operator calls __getitem__ on class that has __len__ and __iter__ defined
Dino Viehland
dinov at microsoft.com
Thu Nov 20 09:13:14 PST 2008
Oh, on the formatting - CodePlex actually preserves the white space it just doesn't display it. It's annoying but I'm not aware of a way to make it actually display other than clicking Edit which I think only we can do :(.
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Glenn Jones
Sent: Thursday, November 20, 2008 7:23 AM
To: users at lists.ironpython.com
Subject: [IronPython] in operator calls __getitem__ on class that has __len__ and __iter__ defined
Yet another weirdness, but not a blocker for us:
With this object:
class o(object):
def __iter__(self):
print "iter"
return iter([1, 2, 3])
def __getitem__(self, index):
print "getitem"
return [1, 2, 3][index]
def __len__(self):
print "len"
return 3
CPython:
>>> p = o()
>>> 1 in p
iter
True
IronPython 2 source drop 43741:
>>> p = o()
>>> 1 in p
getitem
True
It looks like CPython is treating it like a sequence and IronPython 2 is treating it like a dict.
We have worked around this by implementing __contains__
Raised as Issue 19678 on CodePlex.
PS: How can we format code blocks on CodePlex?
Glenn & Orestis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20081120/d82b99f2/attachment-0001.htm>
More information about the Users
mailing list