基于每个对象的monkeypatch __magic__方法。 另外,IIRC,这种行为在Python 3中发生了变化。Other than implementing a custom __cmp__ function ? Not AFAIK. FWIW, youdon''t necessarily need to fiddle with inheritance - you can alsomonkeypatch your classes (nb : for new-style objects, you cannotmonkeypatch __magic__ methods on a per-object basis).Also, IIRC, this behaviour has changed in Python 3. As Bruno说,这将在3.0中改变: #3.0As Bruno says, this will change in 3.0:# 3.0 ........ Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> TypeError:不可订的类型: A()< float() 对于2.5及以上,你可以提供一个关键功能作为解决方法: #2.5Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: unorderable types: A() < float()For 2.5 and above you can provide a key function as a workaround:# 2.5 ........ Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> TypeError:float()参数必须是字符串或数字 PeterTraceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: float() argument must be a string or a numberPeter 你确定你不想使用静态类型的语言 只是通过检查源代码来捕获所有类型错误?Are you sure you don''t want to use a statically typed language thatcaptures all type errors just by inspecting your source code? 这篇关于默认对象比较被认为有害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!