New-style classes, iter and PySequence_Check
Steven Bethard
steven.bethard at gmail.com
Thu Apr 14 17:18:20 EDT 2005
Tuure Laurinolli wrote:
> Someone pasted the original version of the following code snippet on
> #python today. I started investigating why the new-style class didn't
> work as expected, and found that at least some instances of new-style
> classes apparently don't return true for PyInstance_Check, which causes
> a problem in PySequence_Check, since it will only do an attribute lookup
> for instances.
>
> Things probably shouldn't be this way. Should I go to python-dev with this?
>
> Demonstration snippet:
For anyone who's curious, here's what the code actually does:
py> args={'a':0}
py> class Args(object):
... def __getattr__(self,attr):
... print "__getattr__:", attr
... return getattr(args,attr)
...
py> class ClassicArgs:
... def __getattr__(self, attr):
... print "__getattr__:", attr
... return getattr(args, attr)
...
py> c = ClassicArgs()
py> i = c.__iter__()
__getattr__: __iter__
py> print i
<dictionary-keyiterator object at 0x0115D920>
py> i = iter(c)
__getattr__: __iter__
py> print i
<dictionary-keyiterator object at 0x01163CA0>
py> a = Args()
py> i = a.__iter__()
__getattr__: __iter__
py> print i
<dictionary-keyiterator object at 0x01163D20>
py> i = iter(a)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "D:\Steve\My Programming\pythonmodules\sitecustomize.py", line
37, in iter
return orig(*args)
TypeError: iteration over non-sequence
STeVe
More information about the Python-list
mailing list