Chú thích thuộc tính 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]

Phần 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

Phần 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

john = Person['John', 25] print[john.age]

Code language: Python [python]

đầu ra

25

Code language: Python [python]

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

print[john.get_age[]]

Code language: Python [python]

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 một đố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

john = Person['John', 25] print[john.age]

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

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

Code language: Python [python]

john = Person['John', 25] print[john.age]

Code language: Python [python]
1 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í

john = Person['John', 25] print[john.age]

Code language: Python [python]
2 để trang trí phương thức

john = Person['John', 25] print[john.age]

Code language: Python [python]
0 như sau

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

Code language: Python [python]

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

john = Person['John', 25] print[john.age]

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

trang trí setter

Phần sau đây bổ sung một phương thức setter [_______12_______5] để gán giá trị cho thuộc tính

john = Person['John', 25] print[john.age]

Code language: Python [python]
6 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 @property def age[self]: return self._age def set_age[self, value]: if value

Chủ Đề