递归限制问题 [英] Recursion limit problems

查看:55
本文介绍了递归限制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我遇到了递归限制问题。我发现

的罪魁祸首与__hash__函数有关,我已分配给

添加到集合中的对象。


基本上我的__hash__功能如下:


def __hash __(自我):

out_int = 0

for物业,自我价值:

out_int ^ =哈希(物业)^哈希(价值)


返回out_int


和这个对象的迭代器是:


def __iter __(self):

属性,值自我.__ dict __。iteritems():

yield属性,值


评论__hash__函数并使用Python提供的默认值

后(我想它是基于在内存中对

对象的位置),递归限制问题消失了。 (这个问题是

即使在将递归限制增加到我的平台,MacOSX的最大值后也会发生。


我我不是那么精通Python,所以我不知道我能做到什么才能克服这个问题,任何想法都深受赞赏。


谢谢!

Hello everyone,

I am runing into recursion limit problems. I have found that the
culprit was related to the __hash__ function that I had assigned to
the objects that were added to a set.

Basically my __hash__ function is the following:

def __hash__(self):
out_int = 0
for property,value in self:
out_int ^= hash( property )^hash( value )

return out_int

And the iterator for this object is:

def __iter__(self):
for property,value in self.__dict__.iteritems():
yield property,value

After commenting the __hash__ function and using the default provided
by Python (I suppose it is based on the position in memory of the
object), the recursion limit problems went away. (This problem was
happening even after increasing the recursion limit to the maximum of
my platform, MacOSX).

I am not that versed in Python, so I don''t know exactly I could do to
overcome this problem, any ideas are deeply appreciated.

Thanks!

推荐答案



" elventear" < el ******* @ gmail.comwrote in message

news:11 ********************** @ u30g2000hsc.googlegr oups.com ...

|大家好,

|

|我正在遇到递归限制问题。我发现

|罪魁祸首与我分配给

|的__hash__函数有关添加到集合中的对象。

|

|基本上我的__hash__功能如下:

|

| def __hash __(self):

| out_int = 0

|对于财产,自我价值:

| out_int ^ = hash(property)^ hash(value)

|

|返回out_int

|

|这个对象的迭代器是:

|

| def __iter __(self):

|对于财产,自我价值.__ dict __。iteritems():

|产量属性,价值

|

|评论__hash__函数并使用默认提供的

|通过Python(我想它是基于

|对象的内存位置),递归限制问题消失了。 (这个问题是

|即使将递归限制增加到最大值后也会发生

|我的平台,MacOSX)。

|

|我不是那种精通Python的人,所以我不知道我能做些什么来支付
|克服了这个问题,任何想法都深受赞赏。


如果没有看到完整的代码和异常追溯,我的猜测就是

你的__hash__以某种方式称自己到期到你对象的refence循环。一个简单的循环示例:

a = []; a.append(a)

现在,列表对象不可清除,但是如果它们是,并且哈希是基于值的b $ b(就像你的那样),则哈希(a) )会调用hash(a)会调用

hash(a)....


建议:在__hash__中输入print id(self)并查看是否你得到一个重复。

并且可能减少递归限制以减少追溯列表;-)


Terry Jan Reedy



"elventear" <el*******@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
| Hello everyone,
|
| I am runing into recursion limit problems. I have found that the
| culprit was related to the __hash__ function that I had assigned to
| the objects that were added to a set.
|
| Basically my __hash__ function is the following:
|
| def __hash__(self):
| out_int = 0
| for property,value in self:
| out_int ^= hash( property )^hash( value )
|
| return out_int
|
| And the iterator for this object is:
|
| def __iter__(self):
| for property,value in self.__dict__.iteritems():
| yield property,value
|
| After commenting the __hash__ function and using the default provided
| by Python (I suppose it is based on the position in memory of the
| object), the recursion limit problems went away. (This problem was
| happening even after increasing the recursion limit to the maximum of
| my platform, MacOSX).
|
| I am not that versed in Python, so I don''t know exactly I could do to
| overcome this problem, any ideas are deeply appreciated.

Without seeing the full code and the exception traceback, my guess is that
your __hash__ somehow calls itself due to a refence loop in your object. A
simple example of a loop:
a = []; a.append(a)
Now, list objects are not hashable, but if they were, and the hash were
value based (like your), then hash(a) would call hash(a) would call
hash(a)....

Suggestion: put print id(self) in __hash__ and see if you get a repeat.
And maybe reduce the recursion limit to reduce the traceback listing ;-)

Terry Jan Reedy



En Fri,2007年5月11日19:17:57 -0300,elventear< el ******* @ gmail.com>
$ b $bescribió:
En Fri, 11 May 2007 19:17:57 -0300, elventear <el*******@gmail.com>
escribió:

我遇到了递归限制问题。我发现

的罪魁祸首与__hash__函数有关,我已分配给

添加到集合中的对象。
I am runing into recursion limit problems. I have found that the
culprit was related to the __hash__ function that I had assigned to
the objects that were added to a set.



正如T. Reedy所说,可能你有一个递归结构。


这些关于__hash__,可变性和字典行为的评论也适用

来设置< http://docs.python.org/ref/customization.html#l2h-196:


" The只有必需的属性是比较相等的对象具有相同的哈希值

;建议以某种方式混合在一起(例如,使用

exclusive或)对象组件的哈希值,这也是对象的比较。如果一个类没有定义一个

__cmp __()方法,它也不应该定义一个__hash __()操作;如果它是
定义__cmp __()或__eq __()而不是__hash __(),它的实例将不会被用作字典键。如果一个类定义了可变对象并且
实现了__cmp __()或__eq __()方法,那么它不应该实现

__hash __(),因为字典实现需要一个键's hash =
值是不可变的(如果对象的哈希值发生变化,它将在

错误的哈希桶中)。


-

Gabriel Genellina

As T. Reedy said, probably you have a recursive structure.

These comments about __hash__, mutability and dict behavior are applicable
to sets too <http://docs.python.org/ref/customization.html#l2h-196:

"The only required property is that objects which compare equal have the
same hash value; it is advised to somehow mix together (e.g., using
exclusive or) the hash values for the components of the object that also
play a part in comparison of objects. If a class does not define a
__cmp__() method it should not define a __hash__() operation either; if it
defines __cmp__() or __eq__() but not __hash__(), its instances will not
be usable as dictionary keys. If a class defines mutable objects and
implements a __cmp__() or __eq__() method, it should not implement
__hash__(), since the dictionary implementation requires that a key''s hash
value is immutable (if the object''s hash value changes, it will be in the
wrong hash bucket)."

--
Gabriel Genellina


5月11日晚上11点54分,特里·里迪 < tjre ... @ udel.eduwrote:
On May 11, 11:54 pm, "Terry Reedy" <tjre...@udel.eduwrote:

>

如果没有看到完整代码和异常追溯,我的猜测是

你的__hash__以某种方式调用自己,因为你的对象有一个refence循环。一个简单的循环示例:

a = []; a.append(a)

现在,列表对象不可清除,但是如果它们是,并且哈希是基于值的b $ b(就像你的那样),则哈希(a) )会调用hash(a)会调用

hash(a)....
>
Without seeing the full code and the exception traceback, my guess is that
your __hash__ somehow calls itself due to a refence loop in your object. A
simple example of a loop:
a = []; a.append(a)
Now, list objects are not hashable, but if they were, and the hash were
value based (like your), then hash(a) would call hash(a) would call
hash(a)....



你是对的,我忘记了在某些情况下,我有一些递归指向源的数据

。谢谢!

You were right, I had forgotten that in some instances I had some data
that recursively pointed to the source. Thanks!


这篇关于递归限制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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