In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize operator overloading of Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement operator overloading of Python".
I. Preface
Operator overloading: defining methods for operators
Overloading means giving new meaning to different functions of the same operator.
Second, heavy load effect
Make custom instances operate like built-in objects and make the program easy to read. Give operators to custom objects new regular operators and special method operators to overload
# @ function: operator overload # @ Description: a firefly class MyInteger: "create a custom integer type" def _ _ init__ (self, data=0): # 1. If the parameter passed in is an integer type, then assign the value # 2 directly. If the type passed in is not an integer, the judgment can be converted to an integer Initial value 0 if isinstance (data, int): self.data = data elif isinstance (data, str) and data.isdecimal (): self.data = int (data) else: self.data = 0 def _ add__ (self, other): if isinstance (other) MyInteger): # returns a copy of the current object return MyInteger (self.data + other.data) # adds the MyInteger type elif isinstance (other, int): return MyInteger (self.data + other) # adds the integer def _ _ radd__ (self, other): return self.__add__ (other) def _ eq__ (self Other): if isinstance (other, MyInteger): return self.data = = other.data elif isinstance (other Int): return self.data = = other else: return False def _ _ str__ (self): "" is printing, Automatically called when str (object): return: used to return the readable string form of the object (suitable for ordinary users) "" return str (self.data) def _ _ repr__ (self): "" to convert the object into a form for the interpreter to read Used to read the underlying inheritance relationship and memory address of the object "" return "[value of custom integer type]: {} address: {}" .format (self.data, id (self.data)) def _ _ sub__ (self) Other): return MyInteger (self.data-other.data) def _ _ del__ (self): print ("current object: + str (self.data) + destroyed") # the program is automatically destroyed after running if _ _ name__ = ='_ main__': num1 = MyInteger (123) num2 = MyInteger (321) num3 = num1 + num2 # equivalent to Num3 = num1.__add__ (num2) print ("num3 =" Num3) num4 = MyInteger ("123") num5 = num4 + 124# add integer type num6 = 124+ num4 # on the right side of the custom object add integer type print (" num5 = ", num5," num6 = ", num6) num7 = MyInteger (1024) num8 = MyInteger (1024) print (" num7 = = num8: " Num7 = = num8) 3. Custom list # @ function: custom list # @ Description: a firefly class MyList: def _ _ init__ (self, data=None): self.data = None if data is None: self.data = [] else: self.data = data def _ getitem__ (self) Index): # Let objects of this class support subscript access to if isinstance (index, int): return self.data [index] elif type (index) is slice: # if the parameter is slice type [10:30:2] print ("start value of slice:", index.start) print ("end value of slice:" Index.stop) print ("step size of slice:", index.stop) return self.data [index] def _ _ setitem__ (self, key, value): self.data [key] = value def _ contains__ (self, item): print ("determine incoming", item "whether in the list element") return self.data.__contains__ (item) def _ _ str__ (self): return str (self.data) def pop (self, index=-1): # delete and return the last element return self.data.pop (index) def _ _ delitem__ (self, key): del self.data [key] def append (self) by default Item): self.data.append (item) if _ _ name__ ='_ _ main__': my_list1 = MyList ([item for item in range (10)]) my_list1 [1] = 1111 print ("display list:", my_list1) print ("slice of list:", my_ List1 [2: 8:2]) print ("Delete and return the last element:" My_list1.pop () del my_list1 [1] print ("Delete elements of specified subscript:", my_list1) output result: display list: [0, 1111, 2, 3, 4, 5, 6, 7, 8, 9] slice start value: 2 slice end value: 8 slice step size: 8 list slice: [2,4] 6] delete and return the last element: 9 delete the specified subscript element: [0, 2, 3, 4, 5, 6, 7, 8] I believe that you have a deeper understanding of "how to achieve operator overloading of Python", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.