Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2何时考虑一个函数“?”大于;或;少于;另一个功能?_Python_Python 2.7_Comparison - Fatal编程技术网

Python 2何时考虑一个函数“?”大于;或;少于;另一个功能?

Python 2何时考虑一个函数“?”大于;或;少于;另一个功能?,python,python-2.7,comparison,Python,Python 2.7,Comparison,我刚刚发现,在Python中使用python2中的运算符、=和比较任意函数是合法的,这些类型的比较是基于对象的id()的值进行的: In [1]: def g(): ...: pass In [2]: def y(): ...: pass In [3]: g > y Out[3]: True In [4]: id(g) Out[4]: 55898312 In [5]: id(y) Out[5]: 54420736 id()。可能正是出于这个原因,Pyt

我刚刚发现,在Python中使用python2中的运算符
=
比较任意函数是合法的,这些类型的比较是基于对象的
id()
的值进行的:

In [1]: def g():
   ...:     pass

In [2]: def y():
   ...:     pass

In [3]: g > y
Out[3]: True

In [4]: id(g)
Out[4]: 55898312

In [5]: id(y)
Out[5]: 54420736
id()。可能正是出于这个原因,Python的开发人员删除了此功能,因此比较Python3中的函数会出现一个错误:

In [3]: g > y
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/xxx/<ipython-input-3-9ebc8ff65838> in <module>()
----> 1 g > y

TypeError: unorderable types: function() > function()
线索是:

武断但一贯

Python通过以下方式进行比较:如果

然后


(我得到的奖励是
False

首先,需要注意的是,这种行为在Python 3中更符合逻辑:

>>> def f(): pass
...
>>> def g(): pass
...
>>> f < g
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: function() < function()
>>def():通过
...
>>>def g():通过
...
>>>f
关于Python 2中的行为,您已经引用了:

大多数其他内置类型的对象比较不相等,除非它们是相同的对象;一个对象比另一个对象小还是大的选择是任意的,但在程序的一次执行中是一致的

这里的关键是顺序是任意的,所以要回答您的问题:

我该如何预测这些看似荒谬的操作的结果

不要试图这样做,文件明确指出,您观察到的任何顺序都是不可靠的,并且可能会因不同的口译员、版本,甚至同一程序的不同执行而发生变化


至于如何在CPython中实际实现这一点,是基于函数的,它本质上是内存中的一个地址。请参见

我相当确定†它使用,从其docstring中可以看出:“返回对象的标识。这保证在同时存在的对象中是唯一的。(提示:这是对象的内存地址)。”

这与您在文档中为不等式运算符找到的不变量是一致的

在我的系统中,它似乎取决于定义函数的顺序。例如:

In [1]: def g():
   ...:     pass
   ...: 
In [2]: def y():
   ...:     pass
   ...: 
In [3]: g > y
Out[3]: False
In [4]: y > g
Out[4]: True
In [5]: (id(g), id(y))
Out[5]: (171432676, 171432844)
In [6]: id(g) > id(y)
Out[6]: False
与:

In [1]: def y():
   ...:     pass
   ...: 
In [2]: def g():
   ...:     pass
   ...: 
In [3]: g > y
Out[3]: True
In [4]: y > g
Out[4]: False
In [5]: (id(g), id(y))
Out[5]: (165088140, 165087972)
In [6]: id(g) > id(y)
Out[6]: True
这方面的细节与Python在其私有堆上的工作方式密切相关,但作为一种任意度量,它用于比较直观上不可排序的对象,例如函数


†至少对于CPython的实施

+1进行了大量的研究……但是,我认为这是一个骗局。至于最后一个问题。。。当你想的时候,我想不出一个合理的用例。是的,但并没有真正回答这个问题。很难比较每个答案的帮助程度,所以我会做出一个任意但一致的选择,接受这个(早起的鸟儿有虫吃)Duh,显而易见的解决方案是任意选择id最高的答案()。但是谢谢:)有趣的是,我在ideone上也得到了
False
,但在本地可用的几个Python 2.x环境中,每个环境中都得到了
True
(我想这强调了以这种方式比较对象是多么随意!)
str > list
id(str) > id(list)
>>> def f(): pass
...
>>> def g(): pass
...
>>> f < g
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: function() < function()
In [1]: def g():
   ...:     pass
   ...: 
In [2]: def y():
   ...:     pass
   ...: 
In [3]: g > y
Out[3]: False
In [4]: y > g
Out[4]: True
In [5]: (id(g), id(y))
Out[5]: (171432676, 171432844)
In [6]: id(g) > id(y)
Out[6]: False
In [1]: def y():
   ...:     pass
   ...: 
In [2]: def g():
   ...:     pass
   ...: 
In [3]: g > y
Out[3]: True
In [4]: y > g
Out[4]: False
In [5]: (id(g), id(y))
Out[5]: (165088140, 165087972)
In [6]: id(g) > id(y)
Out[6]: True