功能比较 [英] Comparison of functions

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

问题描述

玩弄比较功能(不要问),我发现了一些有趣的行为:

(lambda y:y)< (lambda y:y)
错误


再次进行比较,事情变得更奇怪:

(lambda y:y)< (lambda y:y)
True(lambda y:y)< (lambda y:y)
错误


这种行为对于有经验的Python会员来说应该很容易回答,但

可能会让初学者感到非常困惑。 br />

为了初学者的利益,想象我们将第一行扩展为两行,

并提交一些人称之为滥用lambda的内容:我们将它们绑定到

名称。

a = lambda y:y
b = lambda y:y
a
< function< lambda>在0xf70598ec> b
< function< lambda>在0xf7059844>


a和b现在绑定到两个对象,每个对象都是一个匿名的

函数,它碰巧做同样的事情。但是每次你创建

a lambda函数时,你会在内存中某个不可预测的

位置得到一个不同的对象。


这是我的Python知识水平让我失望。我不明白

Python是如何比较这两个对象的,因为既不是b也不是b有丰富的比较方法,甚至是旧式的__cmp__方法。 />
a< b



错误


所以我很困惑Python如何比较两者。


如果我们再次比较a和b,我们将始终得到相同的答案。但是如果我们用/ b
用lambda创建一对新的匿名函数,并进行比较,那么

是每次抽奖的好运,无论第一次比较大还是

小于第二个。

-

史蒂文。

解决方案

Steven D''Aprano写道:

玩弄函数比较(不要问),我发现了一些有趣的不直观的行为:

(lambda y:y)< (lambda y:y)错误

再次进行比较,事情变得更加奇怪:
(lambda y:y)< (lambda y:y)真的
(lambda y:y)< (lambda y:y)


错误

对于有经验的Python会员来说,这种行为应该很容易回答,但
可能会让初学者感到非常困惑。 / blockquote>


初学者不应该比较lambda。


你也不应该。 ;-)


-Peter


Steven D''Aprano写道:

玩弄比较功能(不要问),我发现了一些有趣的不直观行为:

a = lambda y:y
b = lambda y:y
a< function< lambda>在0xf70598ec> b< function< lambda>在0xf7059844> a< b


错误

所以我很困惑Python如何比较两者。




在这种情况下,我觉得对象地址是比较的。但是我太懒了,因为他们懒得在源头检查它。 ;)


然而,doc [1]警告你这样的比较:

""大多数其他类型比较不相等,除非他们是同一个对象;

选择是否一个对象被认为比另一个更小或更大

一个是任意的,但在一次执行

程序时是一致的。 ;""

[1] http: //docs.python.org/ref/comparisons.html


2005年7月30日星期六08:13:26 -0400,Peter Hansen写道:

初学者不应该比较lambdas。

你也不应该。 ; - )




实际上,是的,我应该,因为我正在试图弄清楚那些混乱是什么?
是Python''处理比较。

比较中至少有两种不同的感觉被塞进一个,导致如下这样的瑕疵:

L = []
L.sort()#我们可以对列表进行排序
L.append(1 + 1j)
L.sort()#即使它们包括一个复杂的数字
L.append(1)
L.sort()#但不再是
回溯(最近一次调用最后一次):

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

TypeError:使用<,< =,>,> =

$ b无法比较复数$ b嗯,我没有要求使用比较运算符来比较复数。我要求b $ b对列表进行排序。请不要告诉我,使用比较运算符实现排序是

。这只是意味着

的实现让数字排序与排序顺序混淆。


然后就是:

1> 0
真1 + 0j == 1
真1 + 0j == 1> 0
True 1 + 0j> 0



回溯(最近一次调用最后一次):

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

TypeError:使用<,< =,>,> =


无法比较复数我赞赏Python有丰富的比较对于那些需要它们的人来说。

但是混淆了问题哪一个出现在排序列表中?与

哪个更大?,你会得到各种各样的疣,比如无法排序

列出一些物品,同时能够使无意义的/>
比较像''''。join> = [] .append。


我不知道这个丑陋状态的解决方案是什么。我不确定是否确定是否有解决方案。但是我愿意努力工作以获得
*看*,即使你是在开玩笑,也不会被告知

不要。


-

史蒂文。


Playing around with comparisons of functions (don''t ask), I discovered an
interesting bit of unintuitive behaviour:

(lambda y: y) < (lambda y: y) False

Do the comparison again and things become even more bizarre:
(lambda y: y) < (lambda y: y) True (lambda y: y) < (lambda y: y) False

This behaviour should be easy for experienced Pythonisters to answer, but
will probably confuse beginners greatly.

For the benefit of beginners, imagine we expand the first line into two,
and commit what some people call an abuse of lambdas: we bind them to
names.
a = lambda y: y
b = lambda y: y
a <function <lambda> at 0xf70598ec> b <function <lambda> at 0xf7059844>

a and b are now bound to two objects, each of which is an anonymous
function that just happens to do the same thing. But each time you create
a lambda function, you get a different object at some unpredictable
location in memory.

This is where my level of Python knowledge fails me. I don''t understand
how Python is comparing the two objects since neither a nor b have any
rich comparison methods or even the old-style __cmp__ method.
a < b


False

So I''m puzzled about how Python compares the two.

If we compare a and b again, we will always get the same answer. But if we
create a new pair of anonymous functions with lambda, and compare them, it
is the luck of the draw each time whether the first compares bigger or
smaller than the second.
--
Steven.

解决方案

Steven D''Aprano wrote:

Playing around with comparisons of functions (don''t ask), I discovered an
interesting bit of unintuitive behaviour:

(lambda y: y) < (lambda y: y) False

Do the comparison again and things become even more bizarre:
(lambda y: y) < (lambda y: y) True
(lambda y: y) < (lambda y: y)


False

This behaviour should be easy for experienced Pythonisters to answer, but
will probably confuse beginners greatly.



Beginners should not be comparing lambdas.

Neither should you. ;-)

-Peter


Steven D''Aprano wrote:

Playing around with comparisons of functions (don''t ask), I discovered an
interesting bit of unintuitive behaviour:

a = lambda y: y
b = lambda y: y
a <function <lambda> at 0xf70598ec>b <function <lambda> at 0xf7059844>a < b


False

So I''m puzzled about how Python compares the two.



Seems to me the object addresses are compared in this case. But I''m too
lazy to check it in the source. ;)

However, the doc [1] warns you about such comparisons:
"""Most other types compare unequal unless they are the same object; the
choice whether one object is considered smaller or larger than another
one is made arbitrarily but consistently within one execution of a
program."""
[1] http://docs.python.org/ref/comparisons.html


On Sat, 30 Jul 2005 08:13:26 -0400, Peter Hansen wrote:

Beginners should not be comparing lambdas.

Neither should you. ;-)



Actually, yes I should, because I''m trying to make sense of the mess that
is Python''s handling of comparisons. At least two difference senses of
comparisons is jammed into one, leading to such such warts as these:

L = []
L.sort() # we can sort lists
L.append(1+1j)
L.sort() # even if they include a complex number
L.append(1)
L.sort() # but not any more Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=

Um, I didn''t ask to compare complex numbers using comparison operators. I
asked to sort a list. And please don''t tell me that that sorting is
implemented with comparison operators. That just means that the
implementation is confusing numeric ordering with sort order.

Then there is this:
1 > 0 True 1+0j == 1 True 1+0j == 1 > 0 True 1+0j > 0


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=

I applaud that Python has got rich comparisons for those who need them.
But by confusing the question "which comes first in a sorted list?" with
"which is larger?", you get all sorts of warts like being unable to sort
lists with some objects, while being able to make meaningless
comparisons like ''''.join >= [].append.

I''m not sure what the solution to this ugly state of affairs is. I''m not
even sure if there is a solution. But I''m willing to make a good effort to
*look*, and even though you were joking, I don''t appreciate being told
not to.

--
Steven.


这篇关于功能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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