python重写运算符

class Operator(object):
    def __init__(self, task_id):
        self.task_id = task_id

    def __rshift__(self, other):
        if isinstance(other, Operator):
            print('%s=>%s' % (self.task_id, other.task_id))
        elif isinstance(other, list):
            for item in other:
                print('%s=>%s' % (self.task_id, item.task_id))
        return other

    def __lshift__(self, other):
        if isinstance(other, Operator):
            print('%s<=%s' % (self.task_id, other.task_id))
        elif isinstance(other, list):
            for item in other:
                print('%s<=%s' % (self.task_id, item.task_id))
        return other

    def __rrshift__(self, other):
        self.__lshift__(other)
        return self

    def __rlshift__(self, other):
        self.__rshift__(other)
        return self


if __name__ == '__main__':
    a = Operator('a')
    b = Operator('b')
    c = Operator('c')
    d = Operator('d')
    a >> [b, c] >> d

 

posted @ 2020-05-21 19:15  Mars.wang  阅读(658)  评论(0编辑  收藏  举报