Công cụ trang trí setter trong Python là gì?

Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về trình trang trí thuộc tính Python [@property] và quan trọng hơn là cách thức hoạt động của nó

Giới thiệu về trình trang trí thuộc tính Python

Trong hướng dẫn trước, bạn đã học cách sử dụng lớp thuộc tính để thêm một thuộc tính vào một lớp. Đây là cú pháp của lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
4

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]

Sau đây định nghĩa một lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5 với hai thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
6 và

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]

Để định nghĩa một getter cho thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7, bạn sử dụng lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
4 như thế này

________số 8

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
4 chấp nhận một getter và trả về một đối tượng thuộc tính

Sau đây tạo một thể hiện của lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5 và nhận giá trị của thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 thông qua thể hiện

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
2

đầu ra

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
3

Ngoài ra, bạn có thể gọi trực tiếp phương thức

class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]

Code language: Python [python]
3 của đối tượng

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5 như thế này

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
6

Vì vậy, để lấy

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 của đối tượng

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5, bạn có thể sử dụng thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 hoặc phương thức

class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]

Code language: Python [python]
3. Điều này tạo ra một sự dư thừa không cần thiết

Để tránh sự dư thừa này, bạn có thể đổi tên phương thức

class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]

Code language: Python [python]
3 thành phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
20 như thế này

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
3

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
21 chấp nhận một giá trị có thể gọi được [tuổi] và trả về một giá trị có thể gọi được. Vì vậy, nó là một vật trang trí. Do đó, bạn có thể sử dụng trình trang trí

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
22 để trang trí phương pháp

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
20 như sau

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7

Vì vậy, bằng cách sử dụng trình trang trí

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
22, bạn có thể đơn giản hóa định nghĩa thuộc tính cho một lớp

trang trí setter

Sau đây thêm phương thức setter [______125] để gán giá trị cho thuộc tính

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
26 cho lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
2

Để gán

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
25 cho

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
29 của đối tượng thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7, bạn gọi phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
31 của đối tượng thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 như sau

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
8

Phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
31 chấp nhận một khả năng gọi được và trả về một khả năng gọi khác [một đối tượng

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
4]. Vì vậy, bạn có thể sử dụng decorator

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
35 cho phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
36 như thế này

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
0

Bây giờ, bạn có thể thay đổi phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
36 thành phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
20 và sử dụng thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 trong phương thức

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
60

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
1

Để tóm tắt, bạn có thể sử dụng các công cụ trang trí để tạo thuộc tính bằng cách sử dụng mẫu sau

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
2

Trong mẫu này,

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
61 là thuộc tính riêng và

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
62 là tên thuộc tính

Ví dụ sau sử dụng các bộ trang trí

class property[fget=None, fset=None, fdel=None, doc=None]

Code language: Python [python]
22 để tạo các thuộc tính

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
6 và

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
7 trong lớp

class Person: def __init__[self, name, age]: self.name = name self.age = age

Code language: Python [python]
5

@property trong các lớp Python là gì?

Property[] của Python là cách Pythonic để tránh các phương thức getter và setter chính thức trong mã của bạn . Chức năng này cho phép bạn chuyển thuộc tính lớp thành thuộc tính hoặc thuộc tính được quản lý. Vì thuộc tính [] là một chức năng tích hợp, bạn có thể sử dụng nó mà không cần nhập bất kỳ thứ gì.

@property trong python3 là gì?

Hàm thuộc tính Python[] trả về đối tượng của lớp thuộc tính và nó được sử dụng để tạo thuộc tính của lớp . cú pháp. thuộc tính [fget, fset, fdel, doc] Tham số. get[] – dùng để lấy giá trị của thuộc tính. fset[] – được sử dụng để đặt giá trị của thuộc tính.

Trình trang trí trong Python là gì?

Trình trang trí là mẫu thiết kế trong Python cho phép người dùng thêm chức năng mới vào đối tượng hiện có mà không sửa đổi cấu trúc của đối tượng . Trình trang trí thường được gọi trước khi định nghĩa chức năng bạn muốn trang trí.

Tại sao nên sử dụng getters và setters trong Python?

Về cơ bản, mục đích chính của việc sử dụng getters và setters trong các chương trình hướng đối tượng là để đảm bảo đóng gói dữ liệu . Các biến riêng trong python không thực sự là các trường ẩn như trong các ngôn ngữ hướng đối tượng khác.

Chủ Đề