Hướng dẫn what is __ cmp __ in python? - __ cmp __ trong python là gì?

Trong Python3, sáu nhà khai thác so sánh phong phú

__lt__[self, other] 
__le__[self, other] 
__eq__[self, other] 
__ne__[self, other] 
__gt__[self, other] 
__ge__[self, other] 

phải được cung cấp riêng lẻ. Điều này có thể được viết tắt bằng cách sử dụng functools.total_ordering.

Tuy nhiên, điều này hóa ra khá không thể đọc được và không thực tế hầu hết thời gian. Tuy nhiên, bạn phải đặt các phần mã tương tự trong 2 func - hoặc sử dụng một func trợ giúp nữa.

Vì vậy, chủ yếu tôi thích sử dụng lớp mixin PY3__cmp__ được hiển thị bên dưới. Điều này thiết lập lại khung phương pháp __cmp__ duy nhất, đã và khá rõ ràng và thực tế trong hầu hết các trường hợp. Người ta vẫn có thể ghi đè các so sánh phong phú được chọn.

Ví dụ của bạn sẽ trở thành:

 class point[PY3__cmp__]:
      ... 
      # unchanged code

Lớp mixin py3__cmp__:

PY3 = sys.version_info[0] >= 3
if PY3:
    def cmp[a, b]:
        return [a > b] - [a < b]
    # mixin class for Python3 supporting __cmp__
    class PY3__cmp__:   
        def __eq__[self, other]:
            return self.__cmp__[other] == 0
        def __ne__[self, other]:
            return self.__cmp__[other] != 0
        def __gt__[self, other]:
            return self.__cmp__[other] > 0
        def __lt__[self, other]:
            return self.__cmp__[other] < 0
        def __ge__[self, other]:
            return self.__cmp__[other] >= 0
        def __le__[self, other]:
            return self.__cmp__[other] = 3
if PY3:
    def cmp[a, b]:
        return [a > b] - [a < b]
    # mixin class for Python3 supporting __cmp__
    class PY3__cmp__:   
        def __eq__[self, other]:
            return self.__cmp__[other] == 0
        def __ne__[self, other]:
            return self.__cmp__[other] != 0
        def __gt__[self, other]:
            return self.__cmp__[other] > 0
        def __lt__[self, other]:
            return self.__cmp__[other] < 0
        def __ge__[self, other]:
            return self.__cmp__[other] >= 0
        def __le__[self, other]:
            return self.__cmp__[other] = 3
if PY3:
    def cmp[a, b]:
        return [a > b] - [a < b]
    # mixin class for Python3 supporting __cmp__
    class PY3__cmp__:   
        def __eq__[self, other]:
            return self.__cmp__[other] == 0
        def __ne__[self, other]:
            return self.__cmp__[other] != 0
        def __gt__[self, other]:
            return self.__cmp__[other] > 0
        def __lt__[self, other]:
            return self.__cmp__[other] < 0
        def __ge__[self, other]:
            return self.__cmp__[other] >= 0
        def __le__[self, other]:
            return self.__cmp__[other] = 3
if PY3:
    def cmp[a, b]:
        return [a > b] - [a < b]
    # mixin class for Python3 supporting __cmp__
    class PY3__cmp__:   
        def __eq__[self, other]:
            return self.__cmp__[other] == 0
        def __ne__[self, other]:
            return self.__cmp__[other] != 0
        def __gt__[self, other]:
            return self.__cmp__[other] > 0
        def __lt__[self, other]:
            return self.__cmp__[other] < 0
        def __ge__[self, other]:
            return self.__cmp__[other] >= 0
        def __le__[self, other]:
            return self.__cmp__[other] 

Bài Viết Liên Quan

Chủ Đề