美文网首页
实现对象的比较

实现对象的比较

作者: noteby | 来源:发表于2019-02-21 09:38 被阅读0次
import functools


class WordTest(str):

    def __new__(cls, word):
        return str.__new__(cls, word)

    def __gt__(self, other):
        return len(self) > len(other)

    def __lt__(self, other):
        return len(self) < len(other)

    def __ge__(self, other):
        return len(self) >= len(other)

    def __le__(self, other):
        return len(self) <= len(other)

    def __eq__(self, other):
        return len(self) == len(other)


@functools.total_ordering  # __eq__ and __gt__( or __lt__)
class Word(str):

    def __new__(cls, word):
        return str.__new__(cls, word)

    def __gt__(self, other):
        return len(self) > len(other)

    def __eq__(self, other):
        return len(self) == len(other)


s1 = Word('ball')
s2 = Word('b')
print(s1 > s2)
True

相关文章

  • 实现对象的比较

  • Java中Comparable与Comparator的区别

    相同 Comparable和Comparator都是用来实现对象的比较、排序 要想对象比较、排序,都需要实现Com...

  • Comparable接口&Comparator接口

    Comparable用于比较实现Comparable的类的对象;Comparator用于比较没有实现Compara...

  • Java排序之集合排序

    集合排序 要么让集合的对象具有比较性也就是让集合中的对象实现Comparable接口 自定义比较器实现Com...

  • Swift 获取对象地址

    基于Swift3.0 获取地址的用处 最简单的实现对象比较(比如重载 == 来比较对象) 可行方法,代码如下:

  • Comparable 和Comparator

    Comparable:中文翻译:可比较的 adj自定义的对象通过实现Comparable接口,成为一个可比较的对象...

  • 对象比较

    关于对象比较的实现模式;this在对象比较中的应用。 问题: 如果一个自定义类,要想判断它的两个对象是否相等,那么...

  • Comparable & Comparator

    Java 中提供了两种比较机制,一种是让待比较的对象实现 Comparable 接口,拥有比较的能力,另一种是实现...

  • UIViewAnimation动画与Core Animation

    使用UIView类函数实现 使用CATransition对象来实现 CATransition比较强大,一般可以使用...

  • 静态代理和动态代理

    静态代理 静态代理的实现比较简单,代理类通过实现与目标对象相同的接口,并在类中维护一个代理对象。通过构造器塞入目标...

网友评论

      本文标题:实现对象的比较

      本文链接:https://www.haomeiwen.com/subject/mlwlyqtx.html