Hướng dẫn what does property decorator do in python? - trình trang trí thuộc tính làm gì trong python?

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.
8self__

        class2 selfclass4

    class6class7

    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.
8selfPortal: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

    class6    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.
8self__

        class2 selfclass4

    class6class7

    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.
8selfPortal: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 selfclass4

        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

Output:

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 selfclass4name[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.
8self__

    

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

        class2 selfclass4

        def6]:6]:7]:8

    class6class7

            4class7

    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.
8selfPortal: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 selfclass4

def0

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 def2

def3

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 def5

def6 def7

    7 def9

def6

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;

def6

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


Trang trí trong trang trí tài sản Python là gì?

@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ố.a built-in decorator in Python which is helpful in defining the properties effortlessly without manually calling the inbuilt function property[]. Which is used to return the property attributes of a class from the stated getter, setter and deleter as parameters.

Làm thế nào để tài sản hoạt động Python?

Phương thức thuộc tính [] trong Python cung cấp một giao diện cho các thuộc tính thể hiện. Nó gói gọn các thuộc tính thể hiện và cung cấp một thuộc tính, giống như Java và C#. Phương thức thuộc tính [] lấy các phương thức GET, SET và XÓA làm đối số và trả về một đối tượng của lớp thuộc tính.provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#. The property[] method takes the get, set and delete methods as arguments and returns an object of the property class.

Làm thế nào là trang trí tài sản được thực hiện Python?

Bộ trang trí thuộc tính được triển khai với một mẫu tương tự như hàm my_decorator.Sử dụng cú pháp python @decorator, nó nhận được hàm được trang trí như một đối số, giống như trong ví dụ của tôi: some_func_decorated = my_decorator [some_func].Lưu ý rằng tôi đã thay đổi một số tên chức năng để rõ ràng.with a pattern similar to the my_decorator function. Using the Python @decorator syntax, it receives the decorated function as an argument, just like in my example: some_func_decorated = my_decorator[some_func] . Note that I changed some function names for clarity.

Tài sản và setter trong Python là gì?

@Property được sử dụng để có được giá trị của một thuộc tính riêng tư mà không cần sử dụng bất kỳ phương thức getter nào.Chúng ta phải đặt một dòng @Property trước phương thức mà chúng ta trả về biến riêng tư.Để đặt giá trị của biến riêng, chúng tôi sử dụng @method_name.setter trước phương thức.Chúng ta phải sử dụng nó như một setter.. We have to put a line @property in front of the method where we return the private variable. To set the value of the private variable, we use @method_name. setter in front of the method. We have to use it as a setter.

Bài Viết Liên Quan

Chủ Đề