New-style classes, iter and PySequence_Check

Tuure Laurinolli tuure at laurinolli.net
Thu Apr 14 12:55:01 EDT 2005


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:

args={'a':0}

class Args(object):
     def __getattr__(self,attr):
         print "__getattr__:", attr
         return getattr(args,attr)

class ClassicArgs:
     def __getattr__(self, attr):
         print "__getattr__:", attr
         return getattr(args, attr)

if __name__ == '__main__':
     c = ClassicArgs()
     i = c.__iter__()
     print i
     i = iter(c)
     print i

     a = Args()
     i = a.__iter__()
     print i
     i = iter(a)
     print i



More information about the Python-list mailing list