错误?如果没有,如何解决它? [英] Bug? If not, how to work around it?

查看:68
本文介绍了错误?如果没有,如何解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




考虑以下课程:

class Test(object):
.... def __init __(self,obj):

.... self .__ obj = obj

... .def __getattr __(自我,姓名):

....返回getattr(自我.__ obj,姓名)

....


现在:

a =测试([])
a .__ iter__
< method-wrapper object at 0x0112CF30> iter(a)
Traceback(最近一次调用最后一次):

文件"<交互式输入>",第1行,在?

TypeError:迭代非序列




这是一个错误吗?如果没有,如何编码测试,以便iter看到底层对象的

__iter__?


我最诚挚的问候,

G. Rodrigues


PS:在win2k上测试最新的2.3。

解决方案

在文章< ; u8 ******************************** @ 4ax.com> ;, Gon?alo

罗德里格斯< op ***** @ mail.telepac.pt>写道:

class Test(object):... def __init __(self,obj):
... self .__ obj = obj
... def __getattr __(self,name):
... return getattr(self .__ obj,name)
...

现在:
a =测试([])
a .__ iter__<方法包装器对象,位于0x0112CF30> iter(a)回溯(最近一次调用最后一次):
文件"<交互式输入>",第1行,在?
TypeError:迭代非序列



这是一个错误吗?如果没有,如何编写测试,以便iter看到底层对象的
__iter__?




我猜测iter()正在寻找没有

的__iter__属性通过__getattr__找到它。所以,我尝试将以下

方法添加到Test类:


def __iter __(self):

返回self。 __obj .__ iter__


但是返回了以下错误:

TypeError:iter()返回类型''方法包装器的非迭代器''


我将__iter__方法更改为以下内容,它似乎做了你想要的




def __iter __(自我):

返回iter(self .__ obj)


至于这是否是一个bug,我不知道。


-Mark


文章< u8 **************** ****************@4ax.com>,

Gon?alo Rodrigues< op ***** @ mail.telepac.pt>写道:

class Test(object):... def __init __(self,obj):
... self .__ obj = obj
... def __getattr __(self,name):
... return getattr(self .__ obj,name)
... a = Test([])
a .__ iter __<方法包装器对象,位于0x0112CF30> iter(a)回溯(最近一次调用最后一次):
文件"<交互式输入>",第1行,在?
TypeError:迭代非序列



这是一个错误吗?如果没有,如何对Test进行编码以使iter看到底层对象的
__iter__?




正如Mark猜测的那样,iter()直接转到属性而不是使用

类的__getattr__机制。但是,您可以使用元类拦截它来获得
,但这需要一些花哨的步法才能达到
到实例中以获得自我.__ obj。

-

Aahz(aa**@pythoncraft.com)< *> http://www.pythoncraft.com/


这是Python。我们不太关心理论,除非它与有用的实践相交。

。 --Aahz


2003年8月6日星期三19:46:39 -0400,Terry Reedy < tj ***** @ udel.edu>

写道:


Gon?alo Rodrigues" <运算***** @ mail.telepac.pt>在消息中写道
新闻:up ******************************** @ 4ax.com .. < blockquote class =post_quotes>

>我将__iter__方法更改为以下内容,它似乎与你想要的一样:
>
> def __iter __(self):
> return iter(self .__ obj)

但这正是我不想做的事。



如果你不''我想做什么有效,
为什么要求我们打扰回答?




因为它可能无效。也就是说,我想到的用例是

作为一个对象的代理,可能有也可能没有__iter__。 IOW如果我

坚持__iter__我必须编写第二堂课等等...哦......好吧......

TJR



致以最诚挚的问候,

G. Rodrigues


Hi,

Consider the following class:

class Test(object): .... def __init__(self, obj):
.... self.__obj = obj
.... def __getattr__(self, name):
.... return getattr(self.__obj, name)
....

Now:
a = Test([])
a.__iter__ <method-wrapper object at 0x0112CF30> iter(a) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence



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. Rodrigues

P.S: Tested with the latest 2.3 on win2k.

解决方案

In article <u8********************************@4ax.com>, Gon?alo
Rodrigues <op*****@mail.telepac.pt> wrote:

class Test(object): ... def __init__(self, obj):
... self.__obj = obj
... def __getattr__(self, name):
... return getattr(self.__obj, name)
...

Now:
a = Test([])
a.__iter__ <method-wrapper object at 0x0112CF30> iter(a) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence



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 without
going through __getattr__ to find it. So, I tried adding the following
method 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 what
you want:

def __iter__(self):
return iter(self.__obj)

As to whether this is a bug, I don''t know.

-Mark


In article <u8********************************@4ax.com>,
Gon?alo Rodrigues <op*****@mail.telepac.pt> wrote:

class Test(object):... def __init__(self, obj):
... self.__obj = obj
... def __getattr__(self, name):
... return getattr(self.__obj, name)
... a = Test([])
a.__iter__<method-wrapper object at 0x0112CF30> iter(a)Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence



Is 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 using
the __getattr__ machinery of the class. However, you can intercept it
using a metaclass, but that requires a bit of fancy footwork to reach
down 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 intersects
with useful practice. --Aahz


On Wed, 6 Aug 2003 19:46:39 -0400, "Terry Reedy" <tj*****@udel.edu>
wrote:


"Gon?alo Rodrigues" <op*****@mail.telepac.pt> wrote in message
news:up********************************@4ax.com.. .

>I changed the __iter__ method to the following, and it seems to dowhat >you want:
>
> def __iter__(self):
> return iter(self.__obj)

But this is precisely what I don''t want to do.



If 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 serves
as proxy for an object that may or may not have __iter__. IOW if I
stick __iter__ I have to code a second class, etc... etc... Oh well...
TJR



With my best regards,
G. Rodrigues


这篇关于错误?如果没有,如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆