Một tính năng trang trí trong Python kết thúc trong một chức năng, nối thêm một số chức năng vào mã hiện có và sau đó trả về nó. Các phương pháp và chức năng được biết là có thể gọi được vì chúng có thể được gọi. Do đó, một người trang trí cũng là một người có thể gọi lại có thể gọi được. Điều này còn được gọi là metaprogramming như tại thời điểm biên dịch, một phần của chương trình làm thay đổi một phần khác của chương trình. Lưu ý: Để biết thêm thông tin, hãy tham khảo các nhà trang trí trong Pythonmetaprogramming as at compile time a section of program alters another section of the program. Note: For more information, refer to Decorators in Python
Python @Property Decorator
@Property Decorator là một nhà trang trí tích hợp trong Python, rất hữu ích trong việc xác định các thuộc tính một cách dễ dàng mà không cần gọi thủ công thuộc tính chức năng sẵn có []. Được sử dụng để trả về các thuộc tính thuộc tính của một lớp từ getter, setter và deleter đã nêu dưới dạng tham số. Bây giờ, hãy xem một số ví dụ để minh họa việc sử dụng nhà trang trí @Property trong Python: Ví dụ 1: & NBSP;Now, lets see some examples to illustrate the use of @property decorator in Python: Example 1:
Python
class
Portal:
def
__init__[
______7
self
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.1
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.3
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.5
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
__
class
2 self
class
4
class
6class
7
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
Portal:
2
self
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.1
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
Portal:
7
class
6
0
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
__
class
2 self
class
4
class
6class
7
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
Portal:
2
self
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.1
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
Portal:
7
6
7 self
class
4
self
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.1
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
Portal:
7Output:
GeeksforGeeks ## An error is thrown Traceback [most recent call last]: File "main.py", line 42, in print [p.name] File "main.py", line 16, in name return self.__name AttributeError: 'Portal' object has no attribute '_Portal__name'
6
7 self
class
4name[self] labeled with @property is a getter method, name[self, val] is a setter method as it is used to set the value of the attribute __name and so its labeled with @name.setter. Lastly, the method labeled with @name.deleter is a deleter method which can delete the assigned value by
the setter method. However, deleter is invoked with the help of a keyword del. Example 2:
Python
class
Portal:
def
__init__[
______7
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
__
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.5
class
2 self
class
4
def
6]:
6]:
7]:
8
class
6class
7
4class
7
def
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.8
self
Portal:
2
self
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.1
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
Portal:
7
6
7 self
class
4
def
0
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
def
2def
3
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.2
def
5def
6 def
7
7 def
9
def
6
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.31
Ở đây, bộ trang trí @Property được sử dụng để xác định tên thuộc tính trong cổng thông tin lớp, có ba phương thức [getter, setter và deleter] với tên tương tự i.e, name [], nhưng chúng có số lượng tham số khác nhau. Trong đó, tên phương thức [tự] được dán nhãn với @property là phương thức getter, tên [tự, val] là một phương thức setter vì nó được sử dụng để đặt giá trị của thuộc tính __name và do đó được dán nhãn với @name.setter. Cuối cùng, phương thức được dán nhãn với @name.deleter là phương thức Deleter có thể xóa giá trị được gán bằng phương thức setter. Tuy nhiên, Deleter được viện dẫn với sự trợ giúp của từ khóa del. Ví dụ 2: & nbsp;
def
6
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.31
Output:
The value of the tempereture is set. The value of the temperature is: -270 # An error is thrown Traceback [most recent call last]: File "main.py", line 47, in cel.temp = -300 File "main.py", line 28, in temp raise ValueError["It is a value error."] ValueError: It is a value error.
class
__init__[
3