In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use Python's Property decorator". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to use Python's Property decorator" can help you solve the problem.
1. Basic usage of property decorator
Before we talk about the property decorator, let's look at an example:
Class MyClass: def _ init__ (self, word): self._word = word def word (self): return self._word my = MyClass ('Hello') print (my.word ()) print (my.word)
If you execute this code, you will output the following result:
Hello
The main function of this code is to return a string through the word method. The last line accesses the word method directly. In Python, everything can be treated as an object, and methods are no exception. So you directly output the object form of the word method.
However, the call form of my.word is also a way to access properties, so this code can also be seen as using the word method as a property rather than getting the word object itself. Therefore, if you want to use the word method as a property, you need to use the property decorator. Here's a look at the improved code:
Class MyClass: def _ _ init__ (self, word): self._word = word # turn the word method into an attribute @ property def word (self): return self._word my = MyClass ('Hello') # output Hello print (my.word)
This code modifies the word method with @ property, which turns the word method directly into a property, so you can call the word method in my.word form, run this code, and output Hello.
As we can see, any ordinary Python method can be turned into an attribute with only one line of code.
If the method is decorated with @ property, it can no longer be treated as a method call. For example, the word method can no longer be called in the form of my.word (), otherwise the following exception will be thrown:
2. The principle of property decorator
Many of you may find it amazing why you can turn the Python method into an attribute by directly decorating the method with @ property. This section describes in detail the principle of the property decorator.
First, to understand what property really is, output property using the following code:
Print (property)
The output is as follows:
Obviously, property is a class. The Python decorator is actually a syntax sugar, which essentially uses the Python decorator as a function and passes the method / function modified by the decorator as a parameter value to the decorator function. For example, decorating the word method with @ property is equivalent to wrapping the word method with the following code:
Property (word)
That is, when the word method is modified by @ property, it becomes an instance of the property class.
You can use the following code to demonstrate the principle of the property decorator. In this code, the word method is decorated with @ property, while the new_word method is decorated directly by creating an instance of property.
Class MyClass: def _ init__ (self, word): self._word = word @ property def word (self): return self._word # outputs the type print ('word:') of the decorated word method Type (word)) def new_word (self): return self._word # outputs the type of unmodified new_word method print ('new_word:', type (new_word)) new_word = property (new_word) print (type (new_word)) my = MyClass ("android") print (my.word) print (my.new_word)
If you execute this code, you will output the following:
As you can see from the output, the type of the word method modified by @ property is the property class, while the type of the new_word method not modified by @ property is the function class. And the new_word method wrapped by creating a property instance can also be used as a property, which is equivalent to the following code:
Property def new_word (self): return self._word3. Make attributes writable and deletable
The method decorated with @ property is a read-only property, which can neither be written nor deleted, otherwise an exception will be thrown.
If you set the word property with my.word = 'new', the following exception is thrown.
If you delete the word attribute using del my.word, the following exception is thrown:
In fact, the property class also has setter methods and deleter methods, which can make properties writable and deletable. Take a look at the following code:
Class MyClass: def _ _ init__ (self, word): self._word = word @ property def word (self): return self._word # set writeable attribute @ word.setter def word (self, value): self._word = value # set removable attribute This method @ word.deleter def word (self): print ('delete word') self._word =''# is called when the word property is deleted. Make the new_word method read-write and removable def new_word (self) by creating an instance of new_word: return self._word # makes new_word a read-only property And you need to assign the property instance to a new variable Otherwise, new_word1 = property (new_word) def new_word (self) will be overridden by the following new_word method Value): self._word = value # make new_word writable new_word1 = new_word1.setter (new_word) def new_word (self): print ('delete new word') # turn new_word into a removable attribute new_word = new_word1.deleter (new_word) my = MyClass (' hello') print (my.word) my.word = 'world' # def word (self) Value): print (my.word) del my.word print (my.word) print ('-') my = MyClass ('ios') print (my.new_word) my.new_word =' harmony' print (my.new_word) del my.new_word print (my.new_word) 4. Get the original method
Methods decorated with @ property will be replaced by property instances. So how do you get the original method? This is done through the following three methods of the property class:
(1) fget: get the method modified by @ property or @ property.getter
(2) fset: get the method modified by @ property.setter
(3) fdel: get the method modified by @ property.deleter
In the following example, three original methods of the word property are obtained and called.
Class MyClass: def _ _ init__ (self, word): self._word = word @ property def word (self): return self._word @ word.setter def word (self) Value): self._word = value @ word.deleter def word (self): print ('delete word') self._word =' 'fget_word = word.fget fset_word = word.fset fdel_word = word.fdel my = MyClass (' android') print (my.fget_word ()) my.fset_word ('harmony') print (my.fget_word ()) print (my.fdel_word ())
Execute this code and you will output the following result.
This is the end of the content about "how to use Python's Property decorator". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.