Getters và setters trong python giải thích bằng ví dụ là gì?

Cách "Pythonic" không phải là sử dụng "getters" và "setters", mà sử dụng các thuộc tính đơn giản, như câu hỏi minh họa và

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
1 để xóa [nhưng tên được thay đổi để bảo vệ những người vô tội. nội trang]

value = 'something'

obj.attribute = value  
value = obj.attribute
del obj.attribute

Nếu sau này, bạn muốn sửa đổi cài đặt và nhận, bạn có thể làm như vậy mà không cần phải thay đổi mã người dùng, bằng cách sử dụng trình trang trí

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
2

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute

[Mỗi cách sử dụng trình trang trí sao chép và cập nhật đối tượng thuộc tính trước đó, vì vậy lưu ý rằng bạn nên sử dụng cùng một tên cho mỗi hàm/phương thức đặt, nhận và xóa. ]

Sau khi xác định ở trên, cài đặt ban đầu, nhận và xóa mã là như nhau

obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute

Bạn nên tránh điều này

def set_property[property,value]:  
def get_property[property]:  

Thứ nhất, cách trên không hiệu quả vì bạn không cung cấp đối số cho trường hợp thuộc tính sẽ được đặt thành [thường là

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
3], tức là

class Obj:

    def set_property[self, property, value]: # don't do this
        ...
    def get_property[self, property]:        # don't do this either
        ...

Thứ hai, điều này trùng lặp mục đích của hai phương thức đặc biệt,

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
4 và
class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
5

Thứ ba, chúng ta cũng có các hàm dựng sẵn

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
6 và
class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
7

value = 'something'

obj.attribute = value  
value = obj.attribute
del obj.attribute
2

Trình trang trí

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
8 dùng để tạo getters và setters

Ví dụ: chúng tôi có thể sửa đổi hành vi cài đặt để đặt các hạn chế đối với giá trị được đặt

value = 'something'

obj.attribute = value  
value = obj.attribute
del obj.attribute
4

Nói chung, chúng tôi muốn tránh sử dụng

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
2 và chỉ sử dụng các thuộc tính trực tiếp

Đây là những gì người dùng Python mong đợi. Theo quy tắc ít gây ngạc nhiên nhất, bạn nên cố gắng cung cấp cho người dùng của mình những gì họ mong đợi trừ khi bạn có lý do rất thuyết phục để làm ngược lại

trình diễn

Ví dụ: giả sử chúng tôi cần thuộc tính được bảo vệ của đối tượng là một số nguyên trong khoảng từ 0 đến 100 và ngăn việc xóa thuộc tính đó bằng các thông báo thích hợp để thông báo cho người dùng về cách sử dụng hợp lý của nó

value = 'something'

obj.attribute = value  
value = obj.attribute
del obj.attribute
6

[Lưu ý rằng

obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
0 đề cập đến
obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
1 nhưng các phương thức thuộc tính đề cập đến
obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
2. Điều này là để
obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
0 sử dụng thuộc tính thông qua API công khai, đảm bảo thuộc tính được "bảo vệ". ]

Và cách sử dụng

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
1

Những cái tên có quan trọng không?

vâng họ làm.

obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
4 và
obj = Obj[]
obj.attribute = value  
the_value = obj.attribute
del obj.attribute
5 sao chép tài sản gốc. Điều này cho phép các lớp con sửa đổi hành vi đúng cách mà không làm thay đổi hành vi trong lớp cha

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
4

Bây giờ để nó hoạt động, bạn phải sử dụng các tên tương ứng

class Obj:
    """property demo"""
    #
    @property            # first decorate the getter method
    def attribute[self]: # This getter method name is *the* name
        return self._attribute
    #
    @attribute.setter    # the property decorates with `.setter` now
    def attribute[self, value]:   # name, e.g. "attribute", is the same
        self._attribute = value   # the "value" name isn't special
    #
    @attribute.deleter     # decorate with `.deleter`
    def attribute[self]:   # again, the method name is the same
        del self._attribute
0

Tôi không chắc điều này sẽ hữu ích ở đâu, nhưng trường hợp sử dụng là nếu bạn muốn thuộc tính chỉ nhận, đặt và/hoặc xóa. Có lẽ tốt nhất là dính vào cùng một thuộc tính ngữ nghĩa có cùng tên

Sự kết luận

Bắt đầu với các thuộc tính đơn giản

Nếu sau này bạn cần chức năng xung quanh cài đặt, nhận và xóa, bạn có thể thêm chức năng đó bằng trình trang trí thuộc tính

Trình getter và setter trong Python là gì?

Getters và Setters là gì? . Đây là các phương thức được sử dụng trong Lập trình hướng đối tượng [OOPS] giúp truy cập các thuộc tính riêng tư từ một lớp. người định cư. Đây là các phương thức được sử dụng trong tính năng OOPS giúp thiết lập giá trị cho các thuộc tính riêng tư trong một lớp

phương pháp getters và setters đưa ra ví dụ là gì?

Getters và setters được sử dụng để bảo vệ dữ liệu của bạn, đặc biệt khi tạo lớp . Đối với mỗi biến thể hiện, phương thức getter trả về giá trị của nó trong khi phương thức setter đặt hoặc cập nhật giá trị của nó. Vì điều này, getters và setters còn được gọi là bộ truy cập và bộ biến đổi, tương ứng.

Getter được sử dụng để làm gì trong Python?

Hàm getattr[] trả về giá trị của thuộc tính đã chỉ định từ đối tượng đã chỉ định .

@property trong Python là gì?

Người trang trí @property . Cú pháp của hàm này là. thuộc tính[fget=Không, fset=Không, fdel=Không, doc=Không]a built-in function that creates and returns a property object. The syntax of this function is: property[fget=None, fset=None, fdel=None, doc=None]

Chủ Đề