def cmp_fun(): a, b = 5, 3 print (operator.lt(a,b)) #True Same as a<b. print (operator.le(a, b)) # False print (operator.eq(a,b)) # False print (operator.ne(a,b)) #TRUE print(operator.ge(a,b)) #False Same as a>=b print (operator.gt(a, b)) # True print (operator.__lt__(a, b)) #TRUE print (operator.__le__(a, b)) #TRUE print (operator.__ne__(a, b)) #TRUE Same as a<b. print (operator.__ge__(a, b)) #FALSE print (operator.__gt__(a, b)) #FALSE print (operator.__eq__(a, b))
def specialcases(x): operator.lt(x,3) operator.le(x,3) operator.eq(x,3) operator.ne(x,3) operator.gt(x,3) operator.ge(x,3) is_operator(x,3) operator.__lt__(x,3) operator.__le__(x,3) operator.__eq__(x,3) operator.__ne__(x,3) operator.__gt__(x,3) operator.__ge__(x,3) # the following ones are constant-folded operator.eq(2,3) operator.__gt__(2,3)
def specialcases(x): operator.lt(x, 3) operator.le(x, 3) operator.eq(x, 3) operator.ne(x, 3) operator.gt(x, 3) operator.ge(x, 3) is_operator(x, 3) operator.__lt__(x, 3) operator.__le__(x, 3) operator.__eq__(x, 3) operator.__ne__(x, 3) operator.__gt__(x, 3) operator.__ge__(x, 3) operator.xor(x, 3) # the following ones are constant-folded operator.eq(2, 3) operator.__gt__(2, 3)
def operations_to_functions(operation, a, b): if operation == ">=": return operator.__ge__(a, b) if operation == "<=": return operator.__le__(a, b) if operation == "=": return operator.__eq__(a, b) if operation == ">": return operator.__gt__(a, b) if operation == "<": return operator.__lt__(a, b)
def __lt__(self, value): return operator.__lt__(self._tensor, value)
@Time : 2020/6/20 14:08 @Software: PyCharm @File : operator_learn.py ''' import operator a = 6 b = 7 operator.lt(a, b) #less than小于 operator.le(a, b) #lessthan or equal to小于等于 operator.eq(a, b) #equal to等于 operator.ne(a, b) #not equalto不等于 operator.ge(a, b) #greaterand equal to大于等于 operator.gt(a, b) #greater大于 operator.__le__(a, b) operator.__lt__(a, b) operator.__eq__(a, b) operator.__ne__(a, b) print(operator.__ge__(a, b)) operator.__gt__(a, b)
minutes = self.hour * 60 + self.minute seconds = minutes * 60 + self.second return seconds #def __radd__(self, other): def __add__(self, other): seconds = self.time_to_int() + other.time_to_int() return int_to_time(seconds) def int_to_time(self,seconds): self=Time() minute, self.second = divmod(seconds, 60) self.hour, self.minute = divmod(minute, 60) return self def int_to_time(seconds): self=Time() minute, self.second = divmod(seconds, 60) self.hour, self.minute = divmod(minute, 60) return self start = Time(9, 45) duration=Time(10,20) operator.__lt__(start,duration)
class Foo:pass foo = Foo() class Bar(object):pass bar = Bar() print(type(Foo))#<class 'type'> print(type(foo))#<class '__main__.Foo'> print(type(Bar))#<class 'type'> print(type(bar))#<class '__main__.Bar'> print(isinstance(foo,Foo)) i,j = -4,12 print(operator.lt(i, j)) print(operator.__lt__(i, j)) students = ['dave', 'john', 'jane'] grades = {'john': 'F', 'jane':'A', 'dave': 'C'} print(sorted(students, key=grades.__getitem__)) array1 = [5, 2, 3, 1, 4] print(sorted(array1)) print(array1) array1.sort() print(array1) array2 ={4: 'E',1: 'D', 2: 'B', 3: 'B', 5: 'A'} print(sorted(array2))
import operator print operator.lt(2,4) print operator.__lt__(2,4) print operator.truth(0) a = 1 print id(a) b = 1 print operator.is_(a, b) b = 2 print operator.is_(a, b) a = -1 print id(a) print operator.index(1) print operator.inv(8) print operator.lshift(4, 1) print operator.or_(2,4) print operator.pos(30)
def __lt__(self, other): """self < other""" return __lt__(get_wrapped_object(self), get_wrapped_object(other))
def update_event(self, inp=-1): self.set_output_val(0, operator.__lt__(self.input(0), self.input(1)))
doc=operator.is_.__doc__) is_not = spice(lambda x, y: operator.is_not(x, y), name='is_not', doc=operator.is_not.__doc__) # reversed le = spice(lambda x, y: operator.le(y, x), name='le') __le__ = spice(lambda x, y: operator.__le__(y, x), name='__le__') # reversed lshift = spice(lambda x, y: operator.lshift(y, x), name='lshift') __lshift__ = spice(lambda x, y: operator.__lshift__(y, x), name='__lshift__') # reversed lt = spice(lambda x, y: operator.lt(y, x), name='lt') __lt__ = spice(lambda x, y: operator.__lt__(y, x), name='__lt__') # reversed matmul = spice(lambda x, y: operator.matmul(y, x), name='matmul') __matmul__ = spice(lambda x, y: operator.__matmul__(y, x), name='__matmul__') # reversed mod = spice(lambda x, y: operator.mod(y, x), name='mod') __mod__ = spice(lambda x, y: operator.__mod__(y, x), name='__mod__') mul = spice(lambda x, y: operator.mul(x, y), name='mul', doc=operator.mul.__doc__) __mul__ = spice(lambda x, y: operator.__mul__(x, y), name='__mul__', doc=operator.mul.__doc__)