Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize cmp in python3

Shulou Source: shulou.com Published: 2022-06-01 18:41:03 09月17日 Update

This article mainly shows you "how to achieve cmp in python3", the content is simple and clear, hoping to help you solve your doubts, let the editor lead you to study and learn "how to achieve cmp in python3" this article.

Python3 cmp implementation

Python3 removes the cmp () function, but provides six rich comparison operators, as detailed here

Import operator # first imports the operator module operator.gt (1Magne2) # which means greater than (greater than) operator.ge (1Magne2) # means greater and equal (greater than or equal to) operator.eq (1Magne2) # means equal (equal to) operator.le (1Magne2) # means less and equal (less than or equal to) operator.lt (1Magne2) # means less than (less than) PY3__cmp _ _ mixin class import sysPY3 = sys.version_info [0] > = 3if 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)

Tags: Functions meaning content parameters articles size modules operators learning help operations subclasses letters Boolean numbers easy to understand more organization annotations knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei OPPO Reno Docker NVidia