In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the special functions in Python". In the daily operation, I believe that many people have doubts about how to use the special functions in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the special functions in Python". Next, please follow the editor to study!
What is operator overloading in Python?
The Python operator is used for built-in classes. But the same operator behaves differently for different types. For example, the + operator performs an arithmetic addition to two numbers, merges two lists, and concatenates two strings.
This feature in Python allows the same operator to have different meanings depending on the context, called operator overloading.
So what happens when you use them with objects of a user-defined class?
Example: the following class that attempts to simulate a point in a two-dimensional coordinate system.
Class Point: def _ init__ (self, x = 0, y = 0): self.x = x self.y = y
Now, run the code and try to add two points to Python shell.
P1 = Point (2) p2 = Point (- 1) print (p1 + p2)
Code:
This is a lot of mistakes. TypeError is raised because Python does not know how to add two Point objects together.
However, this can be taught to Python through operator overloading. But first, let's know something about special functions.
2. Special functions in Python
1. What is a special mode?
Class functions that begin with a double underscore are called special functions in Python. This is because they are not ordinary functions. The init__ () function defined above is one of them. It is called each time a new object of this class is created.
two。 Case
Using special functions, you can make your classes compatible with built-in functions.
Example:
P1 = Point (2) print (p1)
The printout did not achieve the desired effect.
However, if you define the str () method in your class, you can control how it prints. Add this to the class of.
Example
Class Point: def _ init__ (self, x = 0, y = 0): self.x = x self.y = y def _ str__ (self): return "({0}, {1})" .format (self.x,self.y)
Now, let print () try the function again.
P1 = Point (3p7) print (p1)
This turns out to be better, and when you use the built-in function str () or, the same method format () is called.
P1 = Point (2,3) print (str (p1)) print (format (p1))
Therefore, when you execute str (p1) or format (p1), Python executes p1.str () internally. Hence the name, the special function. Let's go back to operator overloading.
Overload + operator
To overload the + symbol, you will need to implement the add () function in the class. While having the right, it is also endowed with great responsibility. You can do whatever you like in this function. But the Point object that returns the sum of coordinates is wise.
Example
Class Point: def _ init__ (self, x = 0, y = 0): self.x = x self.y = y def _ str__ (self): return "({0}, {1})" .format (self.x,self.y) def _ add__ (self) Other): X = self.x + other.x y = self.y + other.y return Point (xQuery y)
Test it:
P1 = Point (2) p2 = Point (- 1) print (p1 + p2)
What actually happens is that when you execute p1 + p2, Python calls p1. _ _ add (p2), that is, Point. Add _ (p1and p2). Similarly, you can overload other operators.
IV. Overload comparison operator
Python does not restrict operator overloading to arithmetic operators. You can also overload the comparison operator.
Suppose you want to implement the less than operator in the Point class
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.