In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "python how to use magic methods", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "python how to use magic methods" this article.
Magic method
The magic method can be seen as a conduit for Python. They are called "underlying" methods and are used for some built-in methods, symbols, and operations. A common magic method that you may be familiar with is _ _ init__ (), which is called when we want to initialize a new instance of a class.
You may have seen other common magic methods, such as _ _ str__ and _ _ repr__. There is a whole set of magic methods in Python, and by implementing some of these methods, we can modify the behavior of an object and even make it behave like a built-in data type, such as a number, list, or dictionary.
Let's create a Money class as an example:
Class Money: currency_rates = {'$': 1, 'girls': 0.88,} def _ init__ (self, symbol, amount): self.symbol = symbol self.amount = amount def _ repr__ (self): return'% .2f'% (self.symbol, self.amount) def convert (self) Other): "Convert other amount to our currency" new_amount = (other.amount / self.currency_ rates [other.symbol] * self.currency_ rates [self.symbol]) return Money (self.symbol, new_amount)
This class definition defines a currency exchange rate for a given currency symbol and exchange rate, specifies an initializer (also known as a constructor), and implements _ _ repr__, so when we print this class, we will see a friendly representation, such as $2.00, which is an instance of Money ('$', 2.00) with a currency symbol and amount. Most importantly, it defines a method that allows you to convert between different currencies using different exchange rates.
Open Python shell and assume that we have defined the cost of food in two different currencies, as follows:
> soda_cost = Money ('$', 5.25) > soda_cost $5.25 > pizza_cost = Money ('$', 7.99) > pizza_cost = 7.99
We can use magic methods to make the instances of this class interact with each other. Suppose we want to be able to add two instances of this class together, even if they are in different currencies. To achieve this, we can implement the magic method _ _ add__ on the Money class:
Class Money: #... Previously defined methods... Def _ add__ (self, other): "Add 2 Money instances using'+'" new_amount = self.amount + self.convert (other). Amount return Money (self.symbol, new_amount)
Now we can use this class in a very intuitive way:
> soda_cost = Money ('$', 5.25) > pizza_cost = Money ('pizza_cost', 7.99) > soda_cost + pizza_cost $14.33 > pizza_cost + soda_cost $12.61
When we add the two instances together, we get the result represented by * currency symbols defined. All the transformations are done seamlessly at the bottom. If we want, we can also implement _ _ sub__, for subtraction, _ _ mul__ for multiplication, and so on. Read the Analog Digital Type or Magic method Guide for more information.
We learned that _ _ add__ maps to the built-in operator +. Other magic methods can be mapped to symbols like []. For example, getting an item in a dictionary through an index or key actually uses the _ _ getitem__ method:
> d = {'one': 1,' two': 2} > d ['two'] 2 > d.qualified getitemps _ (' two') 2
Some magic methods even map to built-in functions, such as _ _ len__ () to len ().
Class Alphabet: letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def _ len__ (self): return len (self.letters) > my_alphabet = Alphabet () > len (my_alphabet) 26 is all the content of this article "how to use Magic in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.