它从
early python剩下来,它基本上使一个数字元组成为相同的底层数字类型,例如
>>> type(10)
>>> type(10.0101010)
>>> nums = coerce(10, 10.001010)
>>> type(nums[0])
>>> type(nums[1])
它也允许对象像旧类一样使用数字
(这里使用的一个坏例子是…)
>>> class bad:
... """ Dont do this, even if coerce was a good idea this simply
... makes itself int ignoring type of other ! """
... def __init__(self, s):
... self.s = s
... def __coerce__(self, other):
... return (other, int(self.s))
...
>>> coerce(10, bad("102"))
(102, 10)