一个简单的a == b'挂起'和无限循环? [英] Can a simple a==b 'hang' in and endless loop?

查看:49
本文介绍了一个简单的a == b'挂起'和无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在了解Python的一些更深入细节的过程中,我很好奇,如果有可能写一个''前缀''代码分配给一个和b

一些特别的东西,所以,Python被困在

行的无限循环中:


如果a = = b:打印''确定''


我的意思是,在我理解Python的过程中对我有很大帮助

知道如何导致无限循环的前缀代码看起来像

,如果最终无法编写这样的代码,要知道为什么它不可能是



我自己的第一个粗略想法是创建永远不会结束的生成器并使用

在''==''比较中,但我还不太清楚如何写

并且还使用了发电机,所以我希望通过研究这个特殊情况来获得一些启示。


Claudio

解决方案

Claudio Grondi写道:

在学习Python的一些更深入的细节的过程中,我很好奇是否可以编写一个''前缀''代码分配给a和b
特别的东西,因此,Python被困在一个无限循环的
行中:

如果a == b:print''OK''

我的意思是,在我理解Python的过程中对我有很大帮助
知道如何导致无限循环的前缀代码看起来像




因为==可被相关对象覆盖,==可以做任何事情:

http://docs.python.org/ref/customization.html

__lt__ __div__ F __gt__


Claudio Grondi写道:

在学习Python的一些更深入细节的过程中我很好奇如果这是po可以编写一个''前缀''代码分配给a和b
特别的东西,所以,Python被困在一个无限循环的
行中:

如果a == b:打印''确定''

我的意思是,在我理解Python的过程中对我有所帮助,以了解这些前缀代码如何导致无穷无尽循环可以看起来像
如果最终不可能编写这样的代码,知道为什么它不可能?

我自己的第一个粗略的想法是创建生成器永远不要在''==''对比中结束和使用它们,但是我还没有很好地理解如何编写
并使用生成器,所以我希望从这个特殊情况来研究
来一些启示。



好​​吧,你可以试试这个:

班级事物:
... def __eq __(自我,其他):

...返回其他== self

... a = thing()
b = thing()
a == b
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第3行,在__eq__

文件"< stdin>" ,第3行,在__eq__

文件"< stdin>",第3行,在__eq__

...

文件" ;< stdin>",第3行,在__eq__

文件"< stdin>",第3行,在__eq__中

RuntimeError:超出最大递归深度


那是你的意思吗?或类似的东西:

类东西:
... def __eq __(自我,其他):

...导入时间; time.sleep(1000000)

... a = thing()
b = thing()
a == b




问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


Steve Holden写道:

Claudio Grondi写道:

在关于Python的一些更深入的细节的学习过程我很好奇如果有可能写一个''前缀''代码分配给一个特殊的东西,那么,Python被困在一个无穷无尽的循环
排成一行:

如果a == b:打印''确定''

我的意思是,它会有很多帮助在我理解的过程中向我介绍了Python知道如何导致无限循环的前缀代码看起来如何,如果最终无法编写这样的代码,那就知道为什么这是不可能的?

我自己的第一个粗略想法是创建永不停止的生成器,并在''==''比较中使用它们,但我还不太清楚如何
编写和使用生成器,所以我希望通过研究这个特殊的案例得到一些启示。


好吧,你可以试试这个:

>>> class thing:... def __eq __(self,other):
... return other == self
...>>> a = thing()
>>> b =东西()
>>> a == b回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?/ />文件"< stdin>",第3行,在__eq__中
文件"< stdin>",第3行,在__eq__
文件"< stdin>",第3行,在__eq__
...
文件"< stdin>",第3行,在__eq__
文件"< stdin>",第3行,在__eq__中
RuntimeError:超出最大递归深度>>> <这是你的意思吗?或类似的东西:
>>> class thing:... def __eq __(self,other):
...导入时间; time.sleep(1000000)
...>>> a = thing()
>>> b =东西()
>>> a == b



问候史蒂夫



感谢您的快速回复。


我明白,我已经监督过,正如Fredrik所说,人们可以直接操纵__eq __()作为实现我的最简单方法
请求。


为了解释为什么我对它不满意,我将尝试提供更多

背景信息。很抱歉没有直接这样做,但是已经

声明我已经忘记了使用__eq __()的可能性。


在Python中内置'' ==''运算符(当没有在自己的代码中操作时)

表现不像''==''运算符,例如在C或Javascript中,因为它b / b
迭代数组(即列表)进行多次比较,而不是比较两个''值''的
'。来自C或Javascript的人会想要''==''将''指针''与数组进行比较,而不是迭代

。列表。

通过上述问题的解决方案,我打算有一个例子来说明这是一个无限循环的问题和导致它的问题

考虑到列表(作为数组)和''==''运算符的函数,考虑到C或Javascript时很难找到。

Claudio



In the process of learning about some deeper details of Python I am
curious if it is possible to write a ''prefix'' code assigning to a and b
something special, so, that Python gets trapped in an endless loop in a
line with:

if a==b: print ''OK''

I mean, it would be of much help to me on my way to understanding Python
to know how such prefix code leading to an endless loop can look like
and if it is eventually not possible to write such code, to know why it
is not possible?

My own first rough idea was to create generators which never end and use
them in the ''=='' comparison, but I have not well understood how to write
and use generators yet, so I expect from studying this special case to
come to some enlightenment.

Claudio

解决方案

Claudio Grondi wrote:

In the process of learning about some deeper details of Python I am
curious if it is possible to write a ''prefix'' code assigning to a and b
something special, so, that Python gets trapped in an endless loop in a
line with:

if a==b: print ''OK''

I mean, it would be of much help to me on my way to understanding Python
to know how such prefix code leading to an endless loop can look like



since == can be overridden by the objects involved, == can do anything:

http://docs.python.org/ref/customization.html

__lt__ __div__ F __gt__


Claudio Grondi wrote:

In the process of learning about some deeper details of Python I am
curious if it is possible to write a ''prefix'' code assigning to a and b
something special, so, that Python gets trapped in an endless loop in a
line with:

if a==b: print ''OK''

I mean, it would be of much help to me on my way to understanding Python
to know how such prefix code leading to an endless loop can look like
and if it is eventually not possible to write such code, to know why it
is not possible?

My own first rough idea was to create generators which never end and use
them in the ''=='' comparison, but I have not well understood how to write
and use generators yet, so I expect from studying this special case to
come to some enlightenment.


Well, you could try this:

class thing: ... def __eq__(self, other):
... return other == self
... a = thing()
b = thing()
a == b Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
...
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
RuntimeError: maximum recursion depth exceeded
Was that what you meant? Or something more like:
class thing: ... def __eq__(self, other):
... import time; time.sleep(1000000)
... a = thing()
b = thing()
a == b



regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Steve Holden wrote:

Claudio Grondi wrote:

In the process of learning about some deeper details of Python I am
curious if it is possible to write a ''prefix'' code assigning to a and
b something special, so, that Python gets trapped in an endless loop
in a line with:

if a==b: print ''OK''

I mean, it would be of much help to me on my way to understanding
Python to know how such prefix code leading to an endless loop can
look like and if it is eventually not possible to write such code, to
know why it is not possible?

My own first rough idea was to create generators which never end and
use them in the ''=='' comparison, but I have not well understood how to
write and use generators yet, so I expect from studying this special
case to come to some enlightenment.


Well, you could try this:

>>> class thing: ... def __eq__(self, other):
... return other == self
... >>> a = thing()
>>> b = thing()
>>> a == b Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
...
File "<stdin>", line 3, in __eq__
File "<stdin>", line 3, in __eq__
RuntimeError: maximum recursion depth exceeded >>>
Was that what you meant? Or something more like:
>>> class thing: ... def __eq__(self, other):
... import time; time.sleep(1000000)
... >>> a = thing()
>>> b = thing()
>>> a == b



regards
Steve


Thanks for the quick reply.

I see, that I have overseen, that as Fredrik also stated, one can
directly manipulate __eq__() as the easiest way to achieve what I
requested.

To explain why I am not happy with it, I will try here to give some more
background information. Sorry for not doing it directly, but as already
stated I have forgot about the possibility to use __eq__().

In Python the built in ''=='' operator (when not manipulated in own code)
behaves not as the ''=='' operator e.g. in C or Javascript, because it
iterates over arrays (i.e. lists) doing many comparisons instead of
comparing only two ''values''. Coming from C or Javascript one would
expect ''=='' to compare the ''pointers'' to the arrays and not to iterate
over all elements of the lists.
With the solution to the question above I intended to have an example of
Python code which outcome is an endless loop and the problem causing it
very hard to find if one thinks in terms of C or Javascript when
considering lists (as arrays) and the function of ''=='' operator.

Claudio


这篇关于一个简单的a == b'挂起'和无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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