Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the example code of Python operator overloading

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Python operator overloaded instance code how to write, I believe that many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

What is operator overloading?

Enable objects (instances) generated by custom classes to operate with operators

Function:

Let custom instances operate like built-in objects

Keep the program simple and readable

Assign new rules to operators for custom objects

Overloading of arithmetic operators:

Method name operators and expression descriptions

__add__(self,rhs) self + rhs addition

__sub__(self,rhs) self - rhs subtraction

__mul__(self,rhs) self * rhs multiplication

__truediv__(self,rhs) self / rhs division

__flooriv__(self,rhs) self //rhs Floor Removal

__mod__(self,rhs) self % rhs modulo (remainder)

__pow__(self,rhs) self **rhs exponentiation

For example,__pow__class Achievement(object): def __init__(self,achievement): self.achievement=achievement def __pow__(self,other): return self.achievement**other.achievement achievement01=Achievement(20)achievement02=Achievement(20)print(pow(achievement01,achievement02))

The printout is the power of the two scores 104857600000000000000, and the basic steps for other operator overloading are roughly the same as__pow__.

The cmp function is no longer available in Python 3, replaced by the operator module, which needs to be imported when used in interactive mode.

import operator operator.le(a, b) operator.eq(a, b) operator.ne(a, b) operator.ge(a, b) operator.gt(a, b) operator.__ lt__(a, b) operator.__ le__(a, b) operator.__ eq__(a, b) operator.__ ne__(a, b) operator.__ ge__(a, b) operator.__ gt__(a, b)

These functions are used to replace the previous cmp, first briefly say the meaning of these functions

lt(a,b) corresponds to a=b

The return value of the function is Boolean

But we can reproduce cmp functionality with operator overloading

Example:

class Achievement(object): def __init__(self,achievement01,achievement02): self.achievement01=achievement01 self.achievement02=achievement02 def cmp(self): if self.achievement01>self.achievement02: print(self.achievement01,">",self.achievement02) return 1 elif self.achievement0150

Overloading of the inverse operator

TypeError error occurs when the left side of the operator is a built-in type and the right side of the operator is a custom type. Arithmetic operators cannot be modified because the code implementation operator overload of the built-in type cannot be modified. In this case, the overload of the inverse operator is required.

Overloading of inverse arithmetic operators:

Method name operators and expression descriptions

__radd__(self,lhs) lhs + self addition

__rsub__(self,lhs) lhs - self subtraction

__rmul__(self,lhs) lhs * self multiplication

__rtruediv__(self,lhs) lhs / self division

__rflooriv__(self,lhs) lhs // self floor removal

__rmod__(self,lhs) lhs % self modulo (remainder)

__rpow__(self,lhs) lhs ** self exponentiation

Examples:

class Mylist: def __init__(self, iterable=()): self.data = list(iterable) def __repr__(self): return 'Mylist(%s)' % self.data def __add__(self, lst): print ('__add__called') return Mylist(self.data + lst.data) def __mul__(self, rhs): # rhs is int type, rhs.data cannot be used print ('__mul__called') return Mylist(self.data * rhs) def __rmul__(self, lhs): print("__rmul__called") return Mylist(self.data * lhs) L1 = Mylist([1, 2, 3])L2 = Mylist([4, 5, 6])L3 = 3 * L1print(L3)L1 += L2print(L1)L2 *= 3print(L2) After reading the above, do you know how to write example code for Python operator overloading? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report