致以最诚挚的问候, G. Rodrigues Hi,Consider the following class:Is this a bug? If not, how to code Test such that iter sees the__iter__ of the underlying object?With my best regards,G. RodriguesP.S: Tested with the latest 2.3 on win2k. 解决方案 Is this a bug? If not, how to code Test such that iter sees the __iter__ of the underlying object?I''m guessing that iter() is looking for an __iter__ attribute withoutgoing through __getattr__ to find it. So, I tried adding the followingmethod to the Test class:def __iter__(self):return self.__obj.__iter__but that returned the following error:TypeError: iter() returned non-iterator of type ''method-wrapper''I changed the __iter__ method to the following, and it seems to do whatyou want:def __iter__(self):return iter(self.__obj)As to whether this is a bug, I don''t know.-MarkIs this a bug? If not, how to code Test such that iter sees the__iter__ of the underlying object?As Mark guessed, iter() goes directly to the attribute rather than usingthe __getattr__ machinery of the class. However, you can intercept itusing a metaclass, but that requires a bit of fancy footwork to reachdown into the instance to get self.__obj.--Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/This is Python. We don''t care much about theory, except where it intersectswith useful practice. --AahzIf you don''t want to do what works,why ask us to bother answering?Because it may not work. That is, the use case I have in mind servesas proxy for an object that may or may not have __iter__. IOW if Istick __iter__ I have to code a second class, etc... etc... Oh well...TJRWith my best regards,G. Rodrigues 这篇关于错误?如果没有,如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!