Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Reversed Operations not available in the stdlib operator module. 

3Defining these instead of using lambdas allows us to reference them by name. 

4""" 

5import operator 

6 

7 

8def radd(left, right): 

9 return right + left 

10 

11 

12def rsub(left, right): 

13 return right - left 

14 

15 

16def rmul(left, right): 

17 return right * left 

18 

19 

20def rdiv(left, right): 

21 return right / left 

22 

23 

24def rtruediv(left, right): 

25 return right / left 

26 

27 

28def rfloordiv(left, right): 

29 return right // left 

30 

31 

32def rmod(left, right): 

33 # check if right is a string as % is the string 

34 # formatting operation; this is a TypeError 

35 # otherwise perform the op 

36 if isinstance(right, str): 

37 typ = type(left).__name__ 

38 raise TypeError(f"{typ} cannot perform the operation mod") 

39 

40 return right % left 

41 

42 

43def rdivmod(left, right): 

44 return divmod(right, left) 

45 

46 

47def rpow(left, right): 

48 return right ** left 

49 

50 

51def rand_(left, right): 

52 return operator.and_(right, left) 

53 

54 

55def ror_(left, right): 

56 return operator.or_(right, left) 

57 

58 

59def rxor(left, right): 

60 return operator.xor(right, left)