|
| 1 | +原文:[Comparing types in Python 3](http://eli.thegreenplace.net/2016/comparing-types-in-python-3/) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +好啦,你要开始进行一些Python元编程,然后发现自己需要对类型序列进行排序。不是对象哦,是_类型_。让我们启动一个Python 2提示来试一试: |
| 6 | +```py |
| 7 | +$ python2.7 |
| 8 | +Python 2.7.6 (default, Jun 22 2015, 17:58:13) |
| 9 | +[GCC 4.8.2] on linux2 |
| 10 | +>>> class Foo(object): pass |
| 11 | +... |
| 12 | +>>> class Bar(object): pass |
| 13 | +... |
| 14 | +>>> class Baz(object): pass |
| 15 | +... |
| 16 | +>>> sorted([Bar, Baz, Foo]) |
| 17 | +[<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Baz'>] |
| 18 | +``` |
| 19 | + |
| 20 | +看起来还不错,那么在Python 3中呢? |
| 21 | +```py |
| 22 | +$ python3.4 |
| 23 | +Python 3.4.3+ (3.4:4255ca2f5314, Apr 2 2015, 05:00:06) |
| 24 | +[GCC 4.8.2] on linux |
| 25 | +>>> class Foo: pass |
| 26 | +... |
| 27 | +>>> class Bar: pass |
| 28 | +... |
| 29 | +>>> class Baz: pass |
| 30 | +... |
| 31 | +>>> sorted([Bar, Baz, Foo]) |
| 32 | +Traceback (most recent call last): |
| 33 | + File "<stdin>", line 1, in <module> |
| 34 | +TypeError: unorderable types: type() < type() |
| 35 | +``` |
| 36 | + |
| 37 | +哎呀,怎么回事?稍微深入挖掘一下,可以快速的发现,Python 3移除了[type对象](http://eli.thegreenplace.net/2012/03/30/python-objects-types-classes-and-instances-a-glossary)的比较。在Python 2中,`type`的类型具有`tp_richcompare`函数来进行比较: |
| 38 | +```py |
| 39 | +static PyObject* |
| 40 | +type_richcompare(PyObject *v, PyObject *w, int op) |
| 41 | +{ |
| 42 | + PyObject *result; |
| 43 | + Py_uintptr_t vv, ww; |
| 44 | + int c; |
| 45 | + |
| 46 | + /* Make sure both arguments are types. */ |
| 47 | + if (!PyType_Check(v) || !PyType_Check(w) || |
| 48 | + /* If there is a __cmp__ method defined, let it be called instead |
| 49 | + of our dumb function designed merely to warn. See bug |
| 50 | + #7491. */ |
| 51 | + Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) { |
| 52 | + result = Py_NotImplemented; |
| 53 | + goto out; |
| 54 | + } |
| 55 | + |
| 56 | + /* Py3K warning if comparison isn't == or != */ |
| 57 | + if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE && |
| 58 | + PyErr_WarnEx(PyExc_DeprecationWarning, |
| 59 | + "type inequality comparisons not supported " |
| 60 | + "in 3.x", 1) < 0) { |
| 61 | + return NULL; |
| 62 | + } |
| 63 | + |
| 64 | + /* Compare addresses */ |
| 65 | + vv = (Py_uintptr_t)v; |
| 66 | + ww = (Py_uintptr_t)w; |
| 67 | + switch (op) { |
| 68 | + case Py_LT: c = vv < ww; break; |
| 69 | + case Py_LE: c = vv <= ww; break; |
| 70 | + case Py_EQ: c = vv == ww; break; |
| 71 | + case Py_NE: c = vv != ww; break; |
| 72 | + case Py_GT: c = vv > ww; break; |
| 73 | + case Py_GE: c = vv >= ww; break; |
| 74 | + default: |
| 75 | + result = Py_NotImplemented; |
| 76 | + goto out; |
| 77 | + } |
| 78 | + result = c ? Py_True : Py_False; |
| 79 | + |
| 80 | + /* incref and return */ |
| 81 | + out: |
| 82 | + Py_INCREF(result); |
| 83 | + return result; |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +需要注意的是,type对象是通过数值比较它们的`PyObject`指针来进行比较的。还要注意,在Python 3中,关于type不等的弃用警告消失了。事实上,如果我们窥窃Python 3中的`typeobject.c`,那么你会发现,`tp_richcompare`函数是空的。 |
| 88 | + |
| 89 | +但是可怜的元编程程序员要怎么办呢?还可以使用元类呀! |
| 90 | + |
| 91 | +实际上,解决方法非常简单。对于那些定义了`__lt__`的类型,Python都需要生成完整的比较方法。再次说明,不是对象,是_类型_。在`Foo`、`Bar`及其友元中定义`__lt__`将使得这些类型的对象可以进行比较。我们现在不关心这一点。我们想让_类型_本身可比较,因此,必须在`Foo`的类型(默认是`type`)中定义`__lt__`;我们可以使用一个元类来进行修改。 |
| 92 | + |
| 93 | +这里是一个可能的解决方案: |
| 94 | +```py |
| 95 | +>>> class _ComparableTypeMeta(type): |
| 96 | +... def __lt__(self, other): |
| 97 | +... return id(self) < id(other) |
| 98 | +... |
| 99 | +>>> class Foo(metaclass=_ComparableTypeMeta): pass |
| 100 | +... |
| 101 | +>>> class Bar(metaclass=_ComparableTypeMeta): pass |
| 102 | +... |
| 103 | +>>> class Baz(metaclass=_ComparableTypeMeta): pass |
| 104 | +... |
| 105 | +>>> sorted([Bar, Baz, Foo]) |
| 106 | +[<class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.Baz'>] |
| 107 | +``` |
| 108 | + |
| 109 | +啊,好多了。要让某些类型家族可进行比较,我们提供了一个定义了`__lt__`的公共元类。当Python调用`Foo < Bar` (在排序的过程中被调用), `_ComparableTypeMeta.__lt__`发挥作用,它会基于它们的`id`对类型进行比较 - 类似于Python 2中的地址比较。 |
| 110 | + |
| 111 | +与任何涉及元类的东东一样,这是一个“强大的特性”,应该谨慎使用。 |
| 112 | + |
| 113 | +为了公平起见,这个特殊的问题有一个更为简单的解决方法。我们可以只是传递一个`key`参数给`sorted`,就像下面一样: |
| 114 | +```py |
| 115 | +sorted([Bar, Baz, Foo], key=id) |
| 116 | +``` |
| 117 | + |
| 118 | +然后,它将在不需要特殊的元类的情况下进行工作。然而,`sorted`不仅仅是我们可能想要排序和比较类型的唯一的地方,而且,它可能会被掩埋在一些处理所有种对象的通用代码中;另外,基于元类的方法会更加通用。 |
| 119 | + |
| 120 | +欲了解更多信息,请参阅: |
| 121 | + |
| 122 | +* [PEP 207](https://www.python.org/dev/peps/pep-0207/),介绍了在Python 2.1中重新出现的富比较。 |
| 123 | +* [Python 3中的富比较方法](https://docs.python.org/3.4/reference/datamodel.html#object.__lt__)文档。 |
| 124 | +* [Python 3 C API中的type对象](https://docs.python.org/3/c-api/typeobj.html)。 |
| 125 | +* [Python对象,类型,类和实例 —— 词汇](http://eli.thegreenplace.net/2012/03/30/python-objects-types-classes-and-instances-a-glossary). |
| 126 | +* [Python的基本类型 - 图表](http://eli.thegreenplace.net/2012/04/03/the-fundamental-types-of-python-a-diagram)。 |
| 127 | +* [Python对象创建顺序](http://eli.thegreenplace.net/2012/04/16/python-object-creation-sequence)。 |
| 128 | +* [Python元类的一些例子](http://eli.thegreenplace.net/2011/08/14/python-metaclasses-by-example)。 |
| 129 | + |
| 130 | +最后,如果你设法挖掘到了关于决定移除`tp_richcompare`的原始讨论(也许在python-dev上,或者是由于一个问题),请让我知道。对于是什么导致了这个决定,我很好奇。 |
0 commit comments