Các lớp dữ liệu Python có thể làm cho các lớp Python của bạn ít dài dòng hơn và đồng thời mạnh hơn. Đây là phần giới thiệu về cách sử dụng lớp dữ liệu trong chương trình Python của bạn
Nhà văn cao cấp, InfoWorld.
Mục lục
Cho xem nhiều hơn
Mọi thứ trong Python là một đối tượng, hay có câu nói như vậy. Nếu bạn muốn tạo các đối tượng tùy chỉnh của riêng mình, với các thuộc tính và phương thức riêng của chúng, bạn sử dụng đối tượng class
của Python để thực hiện điều đó. Nhưng việc tạo các lớp trong Python đôi khi có nghĩa là viết vô số mã soạn sẵn, lặp đi lặp lại để thiết lập thể hiện của lớp từ các tham số được truyền cho nó hoặc để tạo các hàm phổ biến như toán tử so sánh
Dataclasses, được giới thiệu trong Python 3. 7 [và được nhập vào Python 3. 6], cung cấp một cách thuận tiện, ít dài dòng hơn để tạo lớp học. Nhiều điều thông thường bạn làm trong một lớp, chẳng hạn như khởi tạo các thuộc tính từ các đối số được truyền cho lớp, có thể được rút gọn thành một vài hướng dẫn cơ bản
Ví dụ lớp dữ liệu Python
Đây là một ví dụ đơn giản về một lớp thông thường trong Python
class Book:
'''Object for tracking physical books in a collection.'''
def __init__[self, name: str, weight: float, shelf_id:int = 0]:
self.name = name
self.weight = weight # in grams, for calculating shipping
self.shelf_id = shelf_id
def __repr__[self]:
return[f"Book[name={self.name!r},
weight={self.weight!r}, shelf_id={self.shelf_id!r}]"]
Vấn đề đau đầu nhất ở đây là cách mỗi đối số được chuyển đến __init__
phải được sao chép vào các thuộc tính của đối tượng. Điều này không quá tệ nếu bạn chỉ xử lý với Book
, nhưng nếu bạn phải xử lý với _______60_______, Library
,
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
0, v.v. thì sao? Đây là cùng một lớp Python, được triển khai dưới dạng lớp dữ liệu Python
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
Khi bạn chỉ định các thuộc tính, được gọi là các trường, trong một lớp dữ liệu, trình trang trí
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
1 sẽ tự động tạo tất cả mã cần thiết để khởi tạo chúng. Nó cũng lưu giữ thông tin loại cho từng thuộc tính, vì vậy nếu bạn sử dụng trình giả mạo mã như
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
2, nó sẽ đảm bảo rằng bạn đang cung cấp đúng loại biến cho hàm tạo của lớpMột điều khác mà
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
1 làm đằng sau hậu trường là tự động tạo mã cho một số phương thức dunder phổ biến trong lớp. Trong lớp học thông thường ở trên, chúng tôi phải tự tạo
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
4. Trong lớp dữ liệu, trình trang trí
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
1 tạo ra
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
4 cho bạnKhi một lớp dữ liệu được tạo, nó giống hệt về mặt chức năng với một lớp thông thường. Không có hình phạt hiệu suất cho việc sử dụng một dataclass. Chỉ có một hình phạt hiệu suất nhỏ khi khai báo lớp là một lớp dữ liệu và đó là chi phí một lần khi đối tượng lớp dữ liệu được tạo
Khởi tạo lớp dữ liệu Python nâng cao
Trình trang trí lớp dữ liệu có thể lấy. Hầu hết thời gian bạn sẽ không cần cung cấp chúng, nhưng chúng có thể hữu ích cho một số trường hợp cạnh nhất định. Dưới đây là một số cách hữu ích nhất [tất cả đều là
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
7]
8. Tạo các thể hiện của lớp ở chế độ chỉ đọc. Khi dữ liệu đã được chỉ định, nó không thể được sửa đổifrom dataclasses import dataclass @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float shelf_id: int = 0
9. Cho phép các phiên bản của lớp dữ liệu sử dụng ít bộ nhớ hơn bằng cách chỉ cho phép các trường được xác định rõ ràng trong lớpfrom dataclasses import dataclass @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float shelf_id: int = 0
0. Khi được đặt, tất cả các trường cho lớp chỉ là từ khóafrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: str = field[compare=False] weight: float = field[default=0.0, repr=False] shelf_id: int = 0 chapters: List[str] = field[default_factory=list]
Tùy chỉnh các trường lớp dữ liệu Python bằng hàm
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
Cách thức hoạt động mặc định của lớp dữ liệu sẽ phù hợp với phần lớn các trường hợp sử dụng. Tuy nhiên, đôi khi bạn cần tinh chỉnh cách các trường trong lớp dữ liệu của bạn được khởi tạo. Như hình bên dưới, bạn có thể sử dụng hàm
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1 để tinh chỉnh
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
Khi bạn đặt giá trị mặc định thành phiên bản của
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1, nó sẽ thay đổi cách thiết lập trường tùy thuộc vào thông số bạn cung cấp cho
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1. Đây là những tùy chọn được sử dụng phổ biến nhất cho
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1 [có những tùy chọn khác]
6. Đặt giá trị mặc định cho trường. Bạn cần sử dụngfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: str = field[compare=False] weight: float = field[default=0.0, repr=False] shelf_id: int = 0 chapters: List[str] = field[default_factory=list]
6 nếu bạn a] sử dụngfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: str = field[compare=False] weight: float = field[default=0.0, repr=False] shelf_id: int = 0 chapters: List[str] = field[default_factory=list]
1 để thay đổi bất kỳ tham số nào khác cho trường và b] muốn đặt giá trị mặc định cho trường trên giá trị đó. Trong trường hợp này, chúng tôi sử dụngfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: str = field[compare=False] weight: float = field[default=0.0, repr=False] shelf_id: int = 0 chapters: List[str] = field[default_factory=list]
6 để đặtfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: str = field[compare=False] weight: float = field[default=0.0, repr=False] shelf_id: int = 0 chapters: List[str] = field[default_factory=list]
0 thànhfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
1from dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
2. Cung cấp tên của một hàm, không nhận tham số, trả về một số đối tượng để dùng làm giá trị mặc định cho trường. Trong trường hợp này, chúng tôi muốnfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
3 là một danh sách trốngfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
4. Theo mặc định [from dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
5], kiểm soát xem trường được đề cập có hiển thị trongfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
4 được tạo tự động cho lớp dữ liệu hay không. Trong trường hợp này, chúng tôi không muốn trọng lượng của cuốn sách được hiển thị trongfrom dataclasses import dataclass @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float shelf_id: int = 0
4, vì vậy chúng tôi sử dụngfrom dataclasses import dataclass @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float shelf_id: int = 0
8 để bỏ qua nófrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
9. Theo mặc định [from dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
5], bao gồm trường trong các phương thức so sánh được tạo tự động cho lớp dữ liệu. Ở đây, chúng tôi không muốn dùngfrom dataclasses import dataclass, field from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str weight: float = field[default=0.0, repr=False] shelf_id: Optional[int] = field[init=False] chapters: List[str] = field[default_factory=list] condition: str = field[default="Good", compare=False] def __post_init__[self]: if self.condition == "Discarded": self.shelf_id = None else: self.shelf_id = 0
1 như một phần so sánh cho hai cuốn sách, vì vậy chúng tôi đặtfrom dataclasses import dataclass, field, InitVar from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: InitVar[str] = "Good" weight: float = field[default=0.0, repr=False] shelf_id: int = field[init=False] chapters: List[str] = field[default_factory=list] def __post_init__[self, condition]: if condition == "Unacceptable": self.shelf_id = None else: self.shelf_id = 0
2from dataclasses import dataclass, field, InitVar from typing import List @dataclass class Book: '''Object for tracking physical books in a collection.''' name: str condition: InitVar[str] = "Good" weight: float = field[default=0.0, repr=False] shelf_id: int = field[init=False] chapters: List[str] = field[default_factory=list] def __post_init__[self, condition]: if condition == "Unacceptable": self.shelf_id = None else: self.shelf_id = 0
Lưu ý rằng chúng tôi đã phải điều chỉnh thứ tự của các trường để các trường không mặc định xuất hiện trước
Kiểm soát khởi tạo lớp dữ liệu Python
Tại thời điểm này có lẽ bạn đang tự hỏi. Nếu phương thức __init__
của một lớp dữ liệu được tạo tự động, làm cách nào để tôi có quyền kiểm soát quy trình ban đầu để thực hiện các thay đổi chi tiết hơn?
__post_init__
Nhập phương thức
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
4. Nếu bạn bao gồm phương thức
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
4 trong định nghĩa lớp dữ liệu của mình, thì bạn có thể cung cấp hướng dẫn để sửa đổi các trường hoặc dữ liệu thực thể khác
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float = field[default=0.0, repr=False]
shelf_id: Optional[int] = field[init=False]
chapters: List[str] = field[default_factory=list]
condition: str = field[default="Good", compare=False]
def __post_init__[self]:
if self.condition == "Discarded":
self.shelf_id = None
else:
self.shelf_id = 0
Trong ví dụ này, chúng tôi đã tạo một phương thức
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
4 để đặt
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
7 thành
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
8 nếu điều kiện của sách được khởi tạo là
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
9. Lưu ý cách chúng tôi sử dụng
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1 để khởi tạo
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
7 và chuyển class
2 như class
3 đến
from dataclasses import dataclass, field
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: str = field[compare=False]
weight: float = field[default=0.0, repr=False]
shelf_id: int = 0
chapters: List[str] = field[default_factory=list]
1. Điều này có nghĩa là
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
7 sẽ không được khởi tạo trong __init__
initVar
Một cách khác để tùy chỉnh thiết lập lớp dữ liệu Python là sử dụng loại class
7. Điều này cho phép bạn chỉ định một trường sẽ được chuyển đến __init__
rồi đến
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
4, nhưng sẽ không được lưu trữ trong thể hiện của lớpBằng cách sử dụng class
7, bạn có thể nhận các tham số khi thiết lập lớp dữ liệu chỉ được sử dụng trong quá trình khởi tạo. Đây là một ví dụ
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
Việc đặt loại trường thành class
7 [với loại phụ của nó là loại trường thực tế] báo hiệu cho
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
1 không biến trường đó thành trường lớp dữ liệu mà chuyển dữ liệu sang
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
4 dưới dạng đối sốTrong phiên bản này của lớp Book
của chúng tôi, chúng tôi không lưu trữ
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
1 dưới dạng một trường trong thể hiện của lớp. Chúng tôi chỉ sử dụng
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
1 trong giai đoạn khởi tạo. Nếu chúng tôi thấy rằng
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
1 đã được đặt thành __init__
8, thì chúng tôi sẽ đặt
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
7 thành
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
8 — nhưng chúng tôi không lưu trữ
from dataclasses import dataclass, field, InitVar
from typing import List
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
condition: InitVar[str] = "Good"
weight: float = field[default=0.0, repr=False]
shelf_id: int = field[init=False]
chapters: List[str] = field[default_factory=list]
def __post_init__[self, condition]:
if condition == "Unacceptable":
self.shelf_id = None
else:
self.shelf_id = 0
1 chính nó trong thể hiện của lớpKhi nào nên sử dụng lớp dữ liệu Python—và khi nào không sử dụng chúng
Một kịch bản phổ biến cho việc sử dụng các lớp dữ liệu là thay thế cho. Các lớp dữ liệu cung cấp các hành vi giống nhau và hơn thế nữa, đồng thời chúng có thể trở thành bất biến [như các lớp Book
2] bằng cách chỉ sử dụng Book
3 làm công cụ trang trí
Một trường hợp sử dụng khả thi khác là thay thế các từ điển lồng nhau, vốn có thể trở nên vụng về khi làm việc, bằng các thể hiện lớp dữ liệu lồng nhau. Nếu bạn có lớp dữ liệu Library
, với thuộc tính danh sách là Book
5, thì bạn có thể sử dụng lớp dữ liệu Book
6 để điền vào danh sách đó, sau đó thêm các phương thức để dễ dàng truy cập các mục lồng nhau [e. g. , một cuốn sách trên kệ trong một căn phòng cụ thể]
Nhưng không phải mọi lớp Python đều cần phải là một lớp dữ liệu. Nếu bạn đang tạo một lớp chủ yếu như một cách để nhóm một loạt các phương thức tĩnh lại với nhau, chứ không phải để chứa dữ liệu, thì bạn không cần biến lớp đó thành một lớp dữ liệu. Chẳng hạn, một mẫu phổ biến với các trình phân tích cú pháp là có một lớp nhận cây cú pháp trừu tượng, duyệt cây và gửi các lệnh gọi đến các phương thức khác nhau trong lớp dựa trên loại nút. Vì lớp trình phân tích cú pháp có rất ít dữ liệu của riêng nó nên lớp dữ liệu không hữu ích ở đây
Có liên quan
- con trăn
- Phát triển phần mềm
- Ngôn ngữ lập trình
Serdar Yegulalp là một nhà văn cao cấp tại InfoWorld, tập trung vào học máy, container hóa, devops, hệ sinh thái Python và đánh giá định kỳ