Mã nguồn: lib/dataclasses.py Lib/dataclasses.py Nội phân Chính showShow
Mô-đun này cung cấp một bộ trang trí và chức năng để tự động thêm các phương thức đặc biệt được tạo như
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 và
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...9 vào các lớp do người dùng xác định. Nó ban đầu được mô tả trong PEP 557.special methods such as
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 and
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...9 to user-defined classes. It was originally described in PEP 557.
Các biến thành viên để sử dụng trong các phương thức được tạo này được xác định bằng cách sử dụng các chú thích loại PEP 526. Ví dụ: mã này:PEP 526 type annotations. For example, this code:
from dataclasses import dataclass @dataclass class InventoryItem: """Class for keeping track of an item in inventory.""" name: str unit_price: float quantity_on_hand: int = 0 def total_cost[self] -> float: return self.unit_price * self.quantity_on_hand
Sẽ thêm, trong số những thứ khác, một
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 trông giống như:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
Lưu ý rằng phương pháp này được tự động thêm vào lớp: nó không được chỉ định trực tiếp trong định nghĩa
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'1 được hiển thị ở trên.
Mới trong phiên bản 3.7.
Nội dung mô -đun
Xử lý sau khi khởi động[*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False]¶Biến lớpdecorator that is used to add generated special methods to classes, as described below.
Biến chỉ khởi độngtype annotation. With two exceptions described below, nothing in
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 examines the type specified in the variable annotation.
Các trường hợp đóng băng
Di sản¶
Đặt hàng lại các tham số chỉ từ khóa trong __init __ [] ¶
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
Các chức năng nhà máy mặc định
Giá trị mặc định có thể thay đổi
Các lĩnh vực mô tả kiểu
Mô-đun này cung cấp một bộ trang trí và chức năng để tự động thêm các phương thức đặc biệt được tạo như
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 và@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
9 vào các lớp do người dùng xác định. Nó ban đầu được mô tả trong PEP 557.Các biến thành viên để sử dụng trong các phương thức được tạo này được xác định bằng cách sử dụng các chú thích loại PEP 526. Ví dụ: mã này:
Sẽ thêm, trong số những thứ khác, một
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 trông giống như:Lưu ý rằng phương pháp này được tự động thêm vào lớp: nó không được chỉ định trực tiếp trong định nghĩa
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
1 được hiển thị ở trên.Mới trong phiên bản 3.7.
________ 32 ________ 33 ________ 34 [*, init = true, repr = true, eq = true, order = false
Hàm này là một nhà trang trí được sử dụng để thêm các phương pháp đặc biệt được tạo vào các lớp, như được mô tả dưới đây.
Người trang trí
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 kiểm tra lớp học để tìm@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
6s. Một@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
6 được định nghĩa là một biến lớp có chú thích loại. Với hai trường hợp ngoại lệ được mô tả dưới đây, không có gì trong@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 kiểm tra loại được chỉ định trong chú thích biến.Thứ tự của các trường trong tất cả các phương thức được tạo là thứ tự chúng xuất hiện trong định nghĩa lớp.
Nhà trang trí
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 sẽ thêm các phương pháp khác nhau của Dunder vào lớp, được mô tả dưới đây. Nếu bất kỳ phương thức được thêm nào đã tồn tại trong lớp, hành vi phụ thuộc vào tham số, như được ghi lại dưới đây. Người trang trí trả lại cùng một lớp mà nó được gọi trên; Không có lớp mới được tạo ra.Nếu
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 được sử dụng như một trình trang trí đơn giản không có tham số, nó hoạt động như thể nó có các giá trị mặc định được ghi lại trong chữ ký này. Đó là, ba cách sử dụng@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 này tương đương:Nếu
@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
0 và@dataclass class Point: x: int y: int @dataclass class C: mylist: list[Point] p = Point[10, 20] assert asdict[p] == {'x': 10, 'y': 20} c = C[[Point[0, 0], Point[10, 4]]] assert asdict[c] == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
1 đều đúng, theo mặc định,@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
5 sẽ tạo phương thức@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
9 cho bạn. Nếu@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
0 là đúng và@dataclass class Point: x: int y: int @dataclass class C: mylist: list[Point] p = Point[10, 20] assert asdict[p] == {'x': 10, 'y': 20} c = C[[Point[0, 0], Point[10, 4]]] assert asdict[c] == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
1 là sai,@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
9 sẽ được đặt thànhdict[[field.name, getattr[obj, field.name]] for field in fields[obj]]
5, đánh dấu nó không thể vượt qua [đó là, vì nó là có thể thay đổi]. Nếu@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
0 là sai,@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
9 sẽ bị bỏ qua có nghĩa là phương thức@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
9 của siêu lớp sẽ được sử dụng [nếu siêu lớp làdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
06, điều này có nghĩa là nó sẽ quay trở lại băm dựa trên ID].@dataclass class Point: x: int y: int @dataclass class C: mylist: list[Point] p = Point[10, 20] assert asdict[p] == {'x': 10, 'y': 20} c = C[[Point[0, 0], Point[10, 4]]] assert asdict[c] == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
1: Nếu đúng [mặc định là@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
4], việc gán cho các trường sẽ tạo ra một ngoại lệ. Điều này mô phỏng các trường hợp đông lạnh chỉ đọc. Nếudef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
09 hoặcdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
10 được xác định trong lớp, thì@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
6 sẽ được nâng lên. Xem các cuộc thảo luận dưới đây.def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
12: Nếu đúng [mặc định làdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
13], tpledef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
14 sẽ được tạo từ danh sách các tham số đến phương thức@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 được tạo [ngay cả khi@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 không được tạo, xem ở trên]. Nếu sai, hoặc nếudef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
14 đã được xác định trong lớp, thìdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
14 sẽ không được tạo.
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
19: Nếu đúng [giá trị mặc định là@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
4], thì tất cả các trường sẽ được đánh dấu là chỉ từ khóa. Nếu một trường được đánh dấu là chỉ từ khóa, thì ảnh hưởng duy nhất là tham số@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 được tạo từ trường chỉ từ khóa phải được chỉ định với từ khóa khi@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 được gọi. Không có ảnh hưởng đến bất kỳ khía cạnh nào khác của DataClasses. Xem mục nhập bảng chú số để biết chi tiết. Cũng xem phầndef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
23.parameter glossary entry for details. Also see thedef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
23 section.
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
24: Nếu đúng [mặc định là@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
4], thuộc tínhdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
26 sẽ được tạo và lớp mới sẽ được trả về thay vì bản gốc. Nếudef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
26 đã được xác định trong lớp, thì@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
6 sẽ được nâng lên.
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'6s có thể chỉ định giá trị mặc định, sử dụng cú pháp Python bình thường:
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
Trong ví dụ này, cả
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand30 và
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand31 sẽ được bao gồm trong phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được thêm vào, sẽ được định nghĩa là:
def __init__[self, a: int, b: int = 0]:
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 sẽ được nâng lên nếu một trường không có giá trị mặc định theo trường có giá trị mặc định. Điều này đúng cho dù điều này xảy ra trong một lớp duy nhất, hoặc là kết quả của kế thừa lớp học. ________ 33 ________ 135 [*, mặc định = thiếu, default_factory = thiếu, init = true, repr = true, hash = none, so sánh = true[*, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=MISSING]¶
Đối với các trường hợp sử dụng thông thường và đơn giản, không có chức năng nào khác là bắt buộc. Tuy nhiên, có một số tính năng DataClass yêu cầu thêm thông tin trên mỗi trường. Để đáp ứng nhu cầu này để biết thêm thông tin, bạn có thể thay thế giá trị trường mặc định bằng một cuộc gọi đến hàm
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand36 được cung cấp. Ví dụ:
@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
Như được hiển thị ở trên, giá trị
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand37 là đối tượng Sentinel được sử dụng để phát hiện nếu một số tham số được cung cấp bởi người dùng. Sentinel này được sử dụng vì
dict[[field.name, getattr[obj, field.name]] for field in fields[obj]]5 là một giá trị hợp lệ cho một số tham số có ý nghĩa riêng biệt. Không có mã nào nên trực tiếp sử dụng giá trị
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand37.
Các tham số thành
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand36 là:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
41: Nếu được cung cấp, đây sẽ là giá trị mặc định cho trường này. Điều này là cần thiết bởi vìdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
36 tự gọi thay thế vị trí bình thường của giá trị mặc định.def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
43: Nếu được cung cấp, nó phải là một cuộc gọi không có thể gọi được sẽ được gọi khi cần giá trị mặc định cho trường này. Trong số các mục đích khác, điều này có thể được sử dụng để chỉ định các trường có các giá trị mặc định có thể thay đổi, như được thảo luận dưới đây. Đó là một lỗi để chỉ định cảdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
41 vàdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
43.def __init__[self, a: int, b: int = 0]:
3: Nếu đúng [mặc định], trường này được bao gồm dưới dạng tham số cho phương thức@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 được tạo.def __init__[self, a: int, b: int = 0]:
6: Nếu đúng [mặc định], trường này được bao gồm trong chuỗi được trả về bởi phương thức@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
9 được tạo.def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
50: Đây có thể là bool hoặcdict[[field.name, getattr[obj, field.name]] for field in fields[obj]]
5. Nếu đúng, trường này được bao gồm trong phương thức@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
9 được tạo. Nếudict[[field.name, getattr[obj, field.name]] for field in fields[obj]]
5 [mặc định], hãy sử dụng giá trị củadef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
54: Đây thường là hành vi mong đợi. Một lĩnh vực nên được xem xét trong băm nếu nó được sử dụng để so sánh. Đặt giá trị này thành bất cứ điều gì khác ngoàidict[[field.name, getattr[obj, field.name]] for field in fields[obj]]
5 không được khuyến khích.Một lý do có thể để đặt
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
56 nhưngdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
57 sẽ là nếu một trường tốn kém để tính giá trị băm cho, trường đó là cần thiết để kiểm tra bình đẳng và có các trường khác đóng góp vào giá trị băm kiểu loại. Ngay cả khi một trường được loại trừ khỏi băm, nó vẫn sẽ được sử dụng để so sánh.def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
54: Nếu đúng [mặc định], trường này được bao gồm trong các phương thức công bằng và so sánh được tạo [@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
1,@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]
7, et al.].def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
61: Đây có thể là một ánh xạ hoặc không có. Không ai được coi là một dict trống rỗng. Giá trị này được bọc trongdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
62 để làm cho nó chỉ đọc và tiếp xúc trên đối tượngdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
63. Nó hoàn toàn không được sử dụng bởi các lớp dữ liệu và được cung cấp như một cơ chế mở rộng của bên thứ ba. Nhiều bên thứ ba có thể có chìa khóa riêng, để sử dụng làm không gian tên trong siêu dữ liệu.def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand
19: Nếu đúng, trường này sẽ được đánh dấu là chỉ từ khóa. Điều này được sử dụng khi các tham số phương thức@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
8 được tạo được tính toán.
Nếu giá trị mặc định của trường được chỉ định bởi một cuộc gọi đến
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand36, thì thuộc tính lớp cho trường này sẽ được thay thế bằng giá trị
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand41 được chỉ định. Nếu không có
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand41 được cung cấp, thì thuộc tính lớp sẽ bị xóa. Mục đích là sau khi trình trang trí
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 chạy, các thuộc tính lớp đều sẽ chứa các giá trị mặc định cho các trường, giống như chính giá trị mặc định được chỉ định. Ví dụ, sau:
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 20
Thuộc tính lớp
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand70 sẽ là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand71, thuộc tính lớp
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand72 sẽ là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand73 và các thuộc tính lớp
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand74 và
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand75 sẽ không được đặt. Lớp ________ 33 ________ 177¶
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand63 Các đối tượng mô tả từng trường được xác định. Các đối tượng này được tạo bên trong và được trả về bằng phương pháp cấp độ mô-đun
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand79 [xem bên dưới]. Người dùng không bao giờ nên khởi tạo trực tiếp đối tượng
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand63. Các thuộc tính được ghi lại của nó là:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand81: Tên của trường. def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand82: Loại trường. def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand41,def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand43,def __init__[self, a: int, b: int = 0]:3,def __init__[self, a: int, b: int = 0]:6,def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand50,def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand54,def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand61 vàdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand19 có ý nghĩa và giá trị giống hệt như chúng làm trong hàmdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand36.
Các thuộc tính khác có thể tồn tại, nhưng chúng là riêng tư và không được kiểm tra hoặc dựa vào.
________ 33 ________ 193 [class_or_instance] ¶[class_or_instance]¶Trả về một tuple của các đối tượng
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand63 xác định các trường cho dữ liệu này. Chấp nhận một dữ liệu hoặc một thể hiện của một DataClass. Tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 nếu không được thông qua một dữ liệu hoặc thể hiện của một. Không trả lại các trường giả là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand96 hoặc
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand97. ________ 33 ________ 199 [obj, *, dict_factory = dict] ¶[obj, *, dict_factory=dict]¶
Chuyển đổi DataClass
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 thành Dict [bằng cách sử dụng hàm nhà máy
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...01]. Mỗi DataClass được chuyển đổi thành một mô tả của các trường của nó, dưới dạng các cặp
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...02. DataClasses, dicts, danh sách và bộ dữ liệu được đệ quy vào. Các đối tượng khác được sao chép với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...03.
Ví dụ về việc sử dụng
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...04 trên các dữ liệu lồng nhau:
@dataclass class Point: x: int y: int @dataclass class C: mylist: list[Point] p = Point[10, 20] assert asdict[p] == {'x': 10, 'y': 20} c = C[[Point[0, 0], Point[10, 4]]] assert asdict[c] == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
Để tạo một bản sao nông, cách giải quyết sau đây có thể được sử dụng:
dict[[field.name, getattr[obj, field.name]] for field in fields[obj]]
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...04 tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 nếu
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 không phải là một thể hiện dữ liệu. ________ 33 ________ 209 [obj, *, tuple_factory = tuple] ¶[obj, *, tuple_factory=tuple]¶
Chuyển đổi DataClass
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 thành một tuple [bằng cách sử dụng hàm nhà máy
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...11]. Mỗi DataClass được chuyển đổi thành một bộ của các giá trị trường của nó. DataClasses, dicts, danh sách và bộ dữ liệu được đệ quy vào. Các đối tượng khác được sao chép với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...03.
Tiếp tục từ ví dụ trước:
assert astuple[p] == [10, 20] assert astuple[c] == [[[0, 0], [10, 4]],]
Để tạo một bản sao nông, cách giải quyết sau đây có thể được sử dụng:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand0
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...04 tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 nếu
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 không phải là một thể hiện dữ liệu. ________ 33 ________ 209 [obj, *, tuple_factory = tuple] ¶[cls_name, fields, *, bases=[], namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False]¶
Chuyển đổi DataClass
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 thành một tuple [bằng cách sử dụng hàm nhà máy
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...11]. Mỗi DataClass được chuyển đổi thành một bộ của các giá trị trường của nó. DataClasses, dicts, danh sách và bộ dữ liệu được đệ quy vào. Các đối tượng khác được sao chép với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...03.
Tiếp tục từ ví dụ trước:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand1
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...13 tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 nếu
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 không phải là một thể hiện dữ liệu. ________ 33 ________ 217 [cls_name, trường, *, bases = [], không gian tên = none, init = true, repr = true = Sai] ¶[obj, /, **changes]¶
Tạo một dữ liệu mới với tên
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...18, các trường như được định nghĩa trong
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...19, các lớp cơ sở như được đưa ra trong
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...20 và khởi tạo bằng một không gian tên như được đưa ra trong
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...21.
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...19 là một yếu tố có thể có các phần tử là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand81,
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...24 hoặc
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...25. Nếu chỉ
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand81 được cung cấp,
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...27 được sử dụng cho
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand82. Các giá trị của
def __init__[self, a: int, b: int = 0]:3,
def __init__[self, a: int, b: int = 0]:6,
@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]0,
@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]3,
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 207,
@dataclass class Point: x: int y: int @dataclass class C: mylist: list[Point] p = Point[10, 20] assert asdict[p] == {'x': 10, 'y': 20} c = C[[Point[0, 0], Point[10, 4]]] assert asdict[c] == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}1,
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand12,
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand19 và
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand24 có cùng ý nghĩa như chúng trong
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5.
Hàm này không được yêu cầu nghiêm ngặt, bởi vì bất kỳ cơ chế Python nào để tạo một lớp mới với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...39 sau đó có thể áp dụng hàm
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 để chuyển đổi lớp đó thành một dữ liệu. Hàm này được cung cấp như một sự tiện lợi. Ví dụ:
Tương đương với:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand2 ________ 33 ________ 242 [obj, /, ** thay đổi] ¶
Tạo một đối tượng mới cùng loại với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00, thay thế các trường bằng các giá trị từ
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...44. Nếu
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...00 không phải là một lớp dữ liệu, hãy tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206. Nếu các giá trị trong
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...44 không chỉ định các trường, tăng
@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206.________ 33 ________ 263 [OBJ] ¶[obj]¶
Trả về
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand13 Nếu tham số của nó là một dataClass hoặc một thể hiện của một, nếu không hãy trả về
@dataclass class C: mylist: list[int] = field[default_factory=list] c = C[] c.mylist += [1, 2, 3]4.
Nếu bạn cần biết liệu một lớp có phải là một thể hiện của một dữ liệu [và không phải là chính dữ liệu], thì hãy thêm kiểm tra thêm cho
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...66:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand3 ________ 33 ________ 268¶
Một giá trị sentinel biểu thị một mặc định bị thiếu hoặc default_factory.
________ 33 ________ 270¶Một giá trị sentinel được sử dụng như một chú thích loại. Bất kỳ trường nào sau trường giả với loại
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand23 đều được đánh dấu là các trường chỉ từ khóa. Lưu ý rằng một trường giả loại
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand23 bị bỏ qua hoàn toàn. Điều này bao gồm tên của một lĩnh vực như vậy. Theo quy ước, tên của
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...73 được sử dụng cho trường
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand23. Các trường chỉ từ khóa biểu thị các tham số
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 phải được chỉ định là từ khóa khi lớp được khởi tạo.
Trong ví dụ này, các trường
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...76 và
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...77 sẽ được đánh dấu là các trường chỉ từ khóa:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand4
Trong một DataClass duy nhất, đó là một lỗi để chỉ định nhiều hơn một trường có loại là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand23.
Mới trong phiên bản 3.10.
Ngoại lệ ________ 33 ________ 280¶Lớn lên khi một
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand09 hoặc
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand10 được xác định ngầm được gọi trên một dữ liệu được xác định với
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...83. Nó là một lớp con của
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...84.
Xử lý sau khi khởi động
Mã
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo sẽ gọi một phương thức có tên
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50, nếu
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50 được xác định trên lớp. Nó thường sẽ được gọi là
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...88. Tuy nhiên, nếu bất kỳ trường
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand97 nào được xác định, chúng cũng sẽ được chuyển sang
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50 theo thứ tự chúng được xác định trong lớp. Nếu không có phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo, thì
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50 sẽ không được gọi tự động.
Trong số các mục đích sử dụng khác, điều này cho phép khởi tạo các giá trị trường phụ thuộc vào một hoặc nhiều trường khác. Ví dụ:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand5
Phương pháp
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo bởi
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 không gọi các phương thức lớp cơ sở
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8. Nếu lớp cơ sở có phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 phải được gọi, thì thông thường gọi phương thức này theo phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand6
Tuy nhiên, lưu ý rằng nói chung, các phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 do DataClass tạo ra không cần phải gọi, vì DataClass dẫn xuất sẽ chăm sóc việc khởi tạo tất cả các trường của bất kỳ lớp cơ sở nào là chính dữ liệu.
Xem phần bên dưới trên các biến chỉ dành cho init để biết các cách để chuyển các tham số sang
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50. Cũng xem cảnh báo về cách
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...51 xử lý các trường
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...55.
Biến lớp
Một trong hai nơi
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 thực sự kiểm tra loại trường là xác định xem một trường có phải là biến lớp như được định nghĩa trong PEP 526 hay không. Nó làm điều này bằng cách kiểm tra xem loại trường là
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'03. Nếu một trường là
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand96, nó sẽ bị loại khỏi xem xét như một trường và bị bỏ qua bởi các cơ chế dữ liệu. Các trường giả
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand96 như vậy không được trả về bởi hàm
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand79 ở cấp độ mô-đun.PEP 526. It does this by checking if the type of the field is
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'03. If a field is a
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand96, it is excluded from consideration as a field and is ignored by the dataclass mechanisms. Such
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand96 pseudo-fields are not returned by the module-level
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand79 function.
Biến chỉ khởi động
Nơi khác trong đó
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 kiểm tra chú thích loại là để xác định xem một trường có biến chỉ là một biến chỉ dành cho ban đầu hay không. Nó làm điều này bằng cách xem liệu loại trường thuộc loại
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'08. Nếu một trường là một
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand97, nó được coi là một trường giả được gọi là trường chỉ dành cho init. Vì nó không phải là một trường thực sự, nó không được trả về bởi hàm
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand79 ở cấp độ mô-đun. Các trường chỉ dành cho init được thêm vào làm tham số vào phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo và được chuyển sang phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...50 tùy chọn. Chúng không được sử dụng bởi các dữ liệu.
Ví dụ: giả sử một trường sẽ được khởi tạo từ cơ sở dữ liệu, nếu một giá trị không được cung cấp khi tạo lớp:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand7
Trong trường hợp này,
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand79 sẽ trả về
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand63 đối tượng cho
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'15 và
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'16, nhưng không phải cho
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'17.
Các trường hợp đóng băng
Không thể tạo ra các đối tượng Python thực sự bất biến. Tuy nhiên, bằng cách chuyển
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...83 cho người trang trí
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5, bạn có thể mô phỏng tính bất biến. Trong trường hợp đó, DataClasses sẽ thêm các phương thức
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand09 và
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand10 vào lớp. Các phương pháp này sẽ tăng
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'22 khi được gọi.
Có một hình phạt hiệu suất nhỏ khi sử dụng
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...83:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 không thể sử dụng gán đơn giản để khởi tạo các trường và phải sử dụng
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'25.
Di sản¶
Khi DataClass được tạo bởi trình trang trí
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5, nó sẽ xem qua tất cả các lớp cơ sở của lớp trong MRO ngược [nghĩa là bắt đầu từ ____106] và, đối với mỗi dữ liệu mà nó tìm thấy, thêm các trường từ lớp cơ sở đó vào một lớp lập bản đồ các lĩnh vực. Sau khi tất cả các trường lớp cơ sở được thêm vào, nó sẽ thêm các trường riêng vào ánh xạ được đặt hàng. Tất cả các phương thức được tạo sẽ sử dụng ánh xạ kết hợp, được tính toán của các trường. Bởi vì các trường theo thứ tự chèn, các lớp dẫn xuất ghi đè các lớp cơ sở. Một ví dụ:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand8
Danh sách cuối cùng của các trường, theo thứ tự,
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'28,
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...76,
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...77. Loại cuối cùng của
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'28 là
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'32, như được chỉ định trong lớp
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'33.
Phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo cho
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'33 sẽ trông giống như:
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand9
Đặt hàng lại các tham số chỉ từ khóa trong __init __ [] ¶
Sau khi các tham số cần thiết cho
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tính toán, bất kỳ tham số chỉ từ khóa nào cũng được di chuyển đến sau tất cả các tham số thông thường [không chỉ KEYWORD]. Đây là một yêu cầu về cách thực hiện các tham số chỉ từ khóa trong Python: chúng phải đến sau các tham số không chỉ dành cho Keyword.
Trong ví dụ này,
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'37,
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'38 và
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'39 là các trường chỉ từ khóa và
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'40 và
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'41 là các trường thường xuyên:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...0
Phương thức
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo cho
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'43 sẽ trông giống như:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...1
Lưu ý rằng các tham số đã được đặt hàng lại từ cách chúng xuất hiện trong danh sách các trường: các tham số có nguồn gốc từ các trường thông thường được theo sau bởi các tham số có nguồn gốc từ các trường chỉ từ khóa.
Thứ tự tương đối của các tham số chỉ từ khóa được duy trì trong danh sách tham số
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được đặt hàng lại.
Các chức năng nhà máy mặc định
Nếu
def __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand36 chỉ địnhdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand43, nó được gọi bằng không đối số khi cần giá trị mặc định cho trường. Ví dụ: để tạo một thể hiện mới của danh sách, sử dụng:@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...2Nếu một trường được loại trừ khỏi
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 [sử dụng@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...55] và trường cũng chỉ địnhdef __init__[self, name: str, unit_price: float, quantity_on_hand: int = 0]: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand43, thì hàm nhà máy mặc định sẽ luôn được gọi từ hàm@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...8 được tạo. Điều này xảy ra bởi vì không có cách nào khác để cung cấp cho trường một giá trị ban đầu.
Giá trị mặc định có thể thay đổi
Python lưu trữ các giá trị biến thành viên mặc định trong các thuộc tính lớp. Hãy xem xét ví dụ này, không sử dụng DataClasses:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...3Lưu ý rằng hai trường hợp của lớp
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'33 chia sẻ cùng một biến lớp@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'28, như mong đợi.Sử dụng DataClasses, nếu mã này hợp lệ:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...4Nó sẽ tạo mã tương tự như:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...5Điều này có vấn đề tương tự như ví dụ ban đầu sử dụng lớp
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'33. Nghĩa là, hai trường hợp của lớp@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'43 không chỉ định giá trị cho@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'28 khi tạo một thể hiện lớp sẽ chia sẻ cùng một bản sao của@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'28. Bởi vì DataClass chỉ sử dụng tạo lớp Python bình thường, họ cũng chia sẻ hành vi này. Không có cách nào chung cho các lớp dữ liệu để phát hiện điều kiện này. Thay vào đó, người trang trí@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'5 sẽ tăng@dataclass class C: x: int y: int = field[repr=False] z: int = field[repr=False, default=10] t: int = 206 nếu phát hiện tham số mặc định của loại@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'59,@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'60 hoặc@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'61. Đây là một giải pháp một phần, nhưng nó bảo vệ chống lại nhiều lỗi phổ biến.Sử dụng các hàm nhà máy mặc định là một cách để tạo các phiên bản mới của các loại có thể thay đổi làm giá trị mặc định cho các trường:
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...6
Các lĩnh vực mô tả kiểu
Các trường được gán các đối tượng mô tả vì giá trị mặc định của chúng có các hành vi đặc biệt sau:descriptor objects as their default value have the following special behaviors:
Giá trị cho trường được chuyển đến phương thức DataClass từ
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
62 được chuyển cho phương thức mô tả ____ ____363 thay vì ghi đè lên đối tượng mô tả.Tương tự, khi nhận hoặc đặt trường, phương thức mô tả
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
64 hoặc@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
63 được gọi là thay vì trả về hoặc ghi đè đối tượng mô tả.Để xác định xem một trường có chứa giá trị mặc định hay không,
@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
66 sẽ gọi phương thức@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
64 của bộ mô tả bằng mẫu truy cập lớp của nó [nghĩa là@dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
68. Nếu mô tả trả về giá trị trong trường hợp này, nó sẽ được sử dụng làm mặc định của trường. Nếu mô tả tăng@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...
84 trong tình huống này, sẽ không có giá trị mặc định nào được cung cấp cho trường.
@dataclass class C: ... @dataclass[] class C: ... @dataclass[init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False] class C: ...7
Lưu ý rằng nếu một trường được chú thích bằng loại mô tả, nhưng không được gán một đối tượng mô tả là giá trị mặc định của nó, trường sẽ hoạt động như một trường bình thường.