Repository navigation
Expand file tree
/
Copy pathdataclasses.po
More file actions
971 lines (854 loc) · 50.1 KB
/
Copy pathdataclasses.po
File metadata and controls
971 lines (854 loc) · 50.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001-2025, Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# python-doc bot, 2025
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.10\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-03 16:02+0000\n"
"PO-Revision-Date: 2025-09-22 15:57+0000\n"
"Last-Translator: python-doc bot, 2025\n"
"Language-Team: Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: zh_CN\n"
"Plural-Forms: nplurals=1; plural=0;\n"
#: ../../library/dataclasses.rst:2
msgid ":mod:`dataclasses` --- Data Classes"
msgstr ":mod:`dataclasses` --- 数据类"
#: ../../library/dataclasses.rst:10
msgid "**Source code:** :source:`Lib/dataclasses.py`"
msgstr "**源码:** :source:`Lib/dataclasses.py`"
#: ../../library/dataclasses.rst:14
msgid ""
"This module provides a decorator and functions for automatically adding "
"generated :term:`special method`\\s such as :meth:`__init__` and "
":meth:`__repr__` to user-defined classes. It was originally described in "
":pep:`557`."
msgstr ""
"这个模块提供了一个装饰器和一些函数,用于自动添加生成的 :term:`special method`,例如 :meth:`__init__` 和 "
":meth:`__repr__` 到用户定义的类。 它最初描述于 :pep:`557` 。"
#: ../../library/dataclasses.rst:19
msgid ""
"The member variables to use in these generated methods are defined using "
":pep:`526` type annotations. For example, this code::"
msgstr "在这些生成的方法中使用的成员变量是使用 :pep:`526` 类型标注来定义的。例如以下代码:"
#: ../../library/dataclasses.rst:34
msgid "will add, among other things, a :meth:`__init__` that looks like::"
msgstr "将在添加的内容中包括如下所示的 :meth:`__init__`::"
#: ../../library/dataclasses.rst:41
msgid ""
"Note that this method is automatically added to the class: it is not "
"directly specified in the ``InventoryItem`` definition shown above."
msgstr "请注意,此方法会自动添加到类中:而不是在如上所示的 ``InventoryItem`` 定义中被直接指定。"
#: ../../library/dataclasses.rst:47
msgid "Module contents"
msgstr "模块内容"
#: ../../library/dataclasses.rst:51
msgid ""
"This function is a :term:`decorator` that is used to add generated "
":term:`special method`\\s to classes, as described below."
msgstr "这个函数是 :term:`decorator` ,用于将生成的 :term:`special method` 添加到类中,如下所述。"
#: ../../library/dataclasses.rst:54
msgid ""
"The :func:`dataclass` decorator examines the class to find ``field``\\s. A "
"``field`` is defined as a class variable that has a :term:`type annotation "
"<variable annotation>`. With two exceptions described below, nothing in "
":func:`dataclass` examines the type specified in the variable annotation."
msgstr ""
":func:`dataclass` 装饰器会检查类以查找 ``field`` —— ``field`` 被定义为具有 :term:`类型标注 "
"<variable annotation>` 的类变量。除了下面描述的两个例外,在 :func:`dataclass` "
"中没有什么东西会去检查这些变量标注成了何种类型。"
#: ../../library/dataclasses.rst:60
msgid ""
"The order of the fields in all of the generated methods is the order in "
"which they appear in the class definition."
msgstr "这些字段在所有生成的方法中的顺序,都是它们在类定义中出现的顺序。"
#: ../../library/dataclasses.rst:63
msgid ""
"The :func:`dataclass` decorator will add various \"dunder\" methods to the "
"class, described below. If any of the added methods already exist in the "
"class, the behavior depends on the parameter, as documented below. The "
"decorator returns the same class that it is called on; no new class is "
"created."
msgstr ""
":func:`dataclass` 装饰器将向类中添加如下的各种 dunder "
"方法。如果所添加的方法已存在于类中,则行为将取决于下面所列出的形参。该装饰器会返回调用它的类;不会创建新的类。"
#: ../../library/dataclasses.rst:69
msgid ""
"If :func:`dataclass` is used just as a simple decorator with no parameters, "
"it acts as if it has the default values documented in this signature. That "
"is, these three uses of :func:`dataclass` are equivalent::"
msgstr ""
"如果 :func:`dataclass` 仅用作没有参数的简单装饰器,它将使用它的函数签名中的默认值。也就是说,这三种 "
":func:`dataclass` 用法是等价的:"
#: ../../library/dataclasses.rst:87
msgid "The parameters to :func:`dataclass` are:"
msgstr ":func:`dataclass` 的参数有:"
#: ../../library/dataclasses.rst:89
msgid ""
"``init``: If true (the default), a :meth:`__init__` method will be "
"generated."
msgstr "``init``: 如果为真值(默认),将生成一个 :meth:`__init__` 方法。"
#: ../../library/dataclasses.rst:92
msgid ""
"If the class already defines :meth:`__init__`, this parameter is ignored."
msgstr "如果类已定义 :meth:`__init__` ,则忽略此参数。"
#: ../../library/dataclasses.rst:95
msgid ""
"``repr``: If true (the default), a :meth:`__repr__` method will be "
"generated. The generated repr string will have the class name and the name "
"and repr of each field, in the order they are defined in the class. Fields "
"that are marked as being excluded from the repr are not included. For "
"example: ``InventoryItem(name='widget', unit_price=3.0, "
"quantity_on_hand=10)``."
msgstr ""
"``repr`` :如果为真值(默认),将生成一个 :meth:`__repr__` 方法。 生成的 repr 字符串将具有类名以及每个字段的名称和 "
"repr ,按照它们在类中定义的顺序。不包括标记为从 repr 中排除的字段。 例如:``InventoryItem(name='widget', "
"unit_price=3.0, quantity_on_hand=10)``。"
#: ../../library/dataclasses.rst:102
msgid ""
"If the class already defines :meth:`__repr__`, this parameter is ignored."
msgstr "如果类已定义 :meth:`__repr__` ,则忽略此参数。"
#: ../../library/dataclasses.rst:105
msgid ""
"``eq``: If true (the default), an :meth:`__eq__` method will be generated. "
"This method compares the class as if it were a tuple of its fields, in "
"order. Both instances in the comparison must be of the identical type."
msgstr ""
"``eq`` :如果为true(默认值),将生成 :meth:`__eq__` "
"方法。此方法将类作为其字段的元组按顺序比较。比较中的两个实例必须是相同的类型。"
#: ../../library/dataclasses.rst:110
msgid ""
"If the class already defines :meth:`__eq__`, this parameter is ignored."
msgstr "如果类已定义 :meth:`__eq__` ,则忽略此参数。"
#: ../../library/dataclasses.rst:113
msgid ""
"``order``: If true (the default is ``False``), :meth:`__lt__`, "
":meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` methods will be "
"generated. These compare the class as if it were a tuple of its fields, in "
"order. Both instances in the comparison must be of the identical type. If "
"``order`` is true and ``eq`` is false, a :exc:`ValueError` is raised."
msgstr ""
"``order`` :如果为真值(默认为 ``False`` ),则 :meth:`__lt__` 、 :meth:`__le__` 、 "
":meth:`__gt__` 和 :meth:`__ge__` 方法将生成。 这将类作为其字段的元组按顺序比较。比较中的两个实例必须是相同的类型。如果 "
"``order`` 为真值并且 ``eq`` 为假值 ,则引发 :exc:`ValueError` 。"
#: ../../library/dataclasses.rst:120
msgid ""
"If the class already defines any of :meth:`__lt__`, :meth:`__le__`, "
":meth:`__gt__`, or :meth:`__ge__`, then :exc:`TypeError` is raised."
msgstr ""
"如果类已经定义了 :meth:`__lt__` 、 :meth:`__le__` 、 :meth:`__gt__` 或者 :meth:`__ge__` "
"中的任意一个,将引发 :exc:`TypeError` 。"
#: ../../library/dataclasses.rst:124
msgid ""
"``unsafe_hash``: If ``False`` (the default), a :meth:`__hash__` method is "
"generated according to how ``eq`` and ``frozen`` are set."
msgstr ""
"``unsafe_hash`` :如果为 ``False`` (默认值),则根据 ``eq`` 和 ``frozen`` 的设置方式生成 "
":meth:`__hash__` 方法。"
#: ../../library/dataclasses.rst:127
msgid ""
":meth:`__hash__` is used by built-in :meth:`hash()`, and when objects are "
"added to hashed collections such as dictionaries and sets. Having a "
":meth:`__hash__` implies that instances of the class are immutable. "
"Mutability is a complicated property that depends on the programmer's "
"intent, the existence and behavior of :meth:`__eq__`, and the values of the "
"``eq`` and ``frozen`` flags in the :func:`dataclass` decorator."
msgstr ""
":meth:`__hash__` 由内置的 :meth:`hash()` 使用,当对象被添加到散列集合(如字典和集合)时。有一个 "
":meth:`__hash__` 意味着类的实例是不可变的。可变性是一个复杂的属性,取决于程序员的意图, :meth:`__eq__` "
"的存在性和行为,以及 :func:`dataclass` 装饰器中 ``eq`` 和 ``frozen`` 标志的值。"
#: ../../library/dataclasses.rst:134
msgid ""
"By default, :func:`dataclass` will not implicitly add a :meth:`__hash__` "
"method unless it is safe to do so. Neither will it add or change an "
"existing explicitly defined :meth:`__hash__` method. Setting the class "
"attribute ``__hash__ = None`` has a specific meaning to Python, as described"
" in the :meth:`__hash__` documentation."
msgstr ""
"默认情况下, :func:`dataclass` 不会隐式添加 :meth:`__hash__` 方法,除非这样做是安全的。 "
"它也不会添加或更改现有的明确定义的 :meth:`__hash__` 方法。 设置类属性 ``__hash__ = None`` 对 Python "
"具有特定含义,如 :meth:`__hash__` 文档中所述。"
#: ../../library/dataclasses.rst:140
msgid ""
"If :meth:`__hash__` is not explicitly defined, or if it is set to ``None``, "
"then :func:`dataclass` *may* add an implicit :meth:`__hash__` method. "
"Although not recommended, you can force :func:`dataclass` to create a "
":meth:`__hash__` method with ``unsafe_hash=True``. This might be the case if"
" your class is logically immutable but can nonetheless be mutated. This is a"
" specialized use case and should be considered carefully."
msgstr ""
"如果 :meth:`__hash__` 没有显式定义,或者它被设为 ``None``,则 :func:`dataclass` *可能* 会添加一个隐式 "
":meth:`__hash__` 方法。 虽然并不推荐,但你可以用 ``unsafe_hash=True`` 来强制 :func:`dataclass`"
" 创建一个 :meth:`__hash__` 方法。 如果你的类在逻辑上不可变但却仍然可被修改那么可能就是这种情况。 "
"这是一个特殊用例并且应当被仔细地考虑。"
#: ../../library/dataclasses.rst:147
msgid ""
"Here are the rules governing implicit creation of a :meth:`__hash__` method."
" Note that you cannot both have an explicit :meth:`__hash__` method in your"
" dataclass and set ``unsafe_hash=True``; this will result in a "
":exc:`TypeError`."
msgstr ""
"以下是隐式创建 :meth:`__hash__` 方法的规则。请注意,你不能在数据类中都使用显式的 :meth:`__hash__` 方法并设置 "
"``unsafe_hash=True`` ;这将导致 :exc:`TypeError` 。"
#: ../../library/dataclasses.rst:152
msgid ""
"If ``eq`` and ``frozen`` are both true, by default :func:`dataclass` will "
"generate a :meth:`__hash__` method for you. If ``eq`` is true and "
"``frozen`` is false, :meth:`__hash__` will be set to ``None``, marking it "
"unhashable (which it is, since it is mutable). If ``eq`` is false, "
":meth:`__hash__` will be left untouched meaning the :meth:`__hash__` method "
"of the superclass will be used (if the superclass is :class:`object`, this "
"means it will fall back to id-based hashing)."
msgstr ""
"如果 ``eq`` 和 ``frozen`` 都是 true,默认情况下 :func:`dataclass` 将为你生成一个 "
":meth:`__hash__` 方法。如果 ``eq`` 为 true 且 ``frozen`` 为 false ,则 "
":meth:`__hash__` 将被设置为 ``None`` ,标记它不可用(因为它是可变的)。如果 ``eq`` 为 false ,则 "
":meth:`__hash__` 将保持不变,这意味着将使用超类的 :meth:`__hash__` 方法(如果超类是 :class:`object` "
",这意味着它将回到基于id的hash)。"
#: ../../library/dataclasses.rst:160
msgid ""
"``frozen``: If true (the default is ``False``), assigning to fields will "
"generate an exception. This emulates read-only frozen instances. If "
":meth:`__setattr__` or :meth:`__delattr__` is defined in the class, then "
":exc:`TypeError` is raised. See the discussion below."
msgstr ""
"``frozen``: 如为真值 (默认值为 ``False``),则对字段赋值将会产生异常。 这模拟了只读的冻结实例。 如果在类中定义了 "
":meth:`__setattr__` 或 :meth:`__delattr__` 则将会引发 :exc:`TypeError`。 参见下文的讨论。"
#: ../../library/dataclasses.rst:165
msgid ""
"``match_args``: If true (the default is ``True``), the ``__match_args__`` "
"tuple will be created from the list of parameters to the generated "
":meth:`__init__` method (even if :meth:`__init__` is not generated, see "
"above). If false, or if ``__match_args__`` is already defined in the class,"
" then ``__match_args__`` will not be generated."
msgstr ""
"``match_args``: 如果为真值 (默认值为 ``True``),则将根据传给生成的 :meth:`__init__` 方法的形参列表来创建 "
"``__match_args__`` 元组 (即使没有生成 :meth:`__init__` 也会创建,见上文)。 如果为假值,或者如果 "
"``__match_args__`` 已在类中定义,则将不生成 ``__match_args__``。"
#: ../../library/dataclasses.rst:174
msgid ""
"``kw_only``: If true (the default value is ``False``), then all fields will "
"be marked as keyword-only. If a field is marked as keyword-only, then the "
"only affect is that the :meth:`__init__` parameter generated from a keyword-"
"only field must be specified with a keyword when :meth:`__init__` is called."
" There is no effect on any other aspect of dataclasses. See the "
":term:`parameter` glossary entry for details. Also see the :const:`KW_ONLY`"
" section."
msgstr ""
"``kw_only``: 如果为真值 (默认值为 ``False``),则所有字段都将被标记为仅限关键字。 "
"如果一个字段被标记为仅限关键字,则其唯一的影响是根据仅限关键字的字段生成的 :meth:`__init__` 形参必须使用调用 "
":meth:`__init__` 时传入的关键字来指定。 对于 dataclass 的任何其它方面都没有影响。 请参阅 "
":term:`parameter` 术语表条目了解详情。 另请参阅 :const:`KW_ONLY` 一节。"
#: ../../library/dataclasses.rst:185
msgid ""
"``slots``: If true (the default is ``False``), :attr:`__slots__` attribute "
"will be generated and new class will be returned instead of the original "
"one. If :attr:`__slots__` is already defined in the class, then "
":exc:`TypeError` is raised."
msgstr ""
"``slots``: 如果为真值 (默认值为 ``False``),则将生成 :attr:`__slots__` 属性并将返回一个新类而非原来的类。 "
"如果 :attr:`__slots__` 已在类中定义,则会引发 :exc:`TypeError`。"
#: ../../library/dataclasses.rst:192
msgid ""
"``field``\\s may optionally specify a default value, using normal Python "
"syntax::"
msgstr "可以用普通的 Python 语法为各个 ``field`` 指定默认值:"
#: ../../library/dataclasses.rst:200
msgid ""
"In this example, both ``a`` and ``b`` will be included in the added "
":meth:`__init__` method, which will be defined as::"
msgstr "在这个例子中, ``a`` 和 ``b`` 都将包含在添加的 :meth:`__init__` 方法中,它们将被定义为::"
#: ../../library/dataclasses.rst:205
msgid ""
":exc:`TypeError` will be raised if a field without a default value follows a"
" field with a default value. This is true whether this occurs in a single "
"class, or as a result of class inheritance."
msgstr ""
"如果在具有默认值的字段之后存在没有默认值的字段,将会引发 :exc:`TypeError`。无论此情况是发生在单个类中还是作为类继承的结果,都是如此。"
#: ../../library/dataclasses.rst:211
msgid ""
"For common and simple use cases, no other functionality is required. There "
"are, however, some dataclass features that require additional per-field "
"information. To satisfy this need for additional information, you can "
"replace the default field value with a call to the provided :func:`field` "
"function. For example::"
msgstr ""
"大多数时候,对于简单常见的用途,前述的功能已经足够了。而有些功能需要字段提供额外的信息来启用。为了满足这种对附加信息的需求,你可以通过调用提供的 "
":func:`field` 函数来替换字段默认值。例如:"
#: ../../library/dataclasses.rst:224
msgid ""
"As shown above, the :const:`MISSING` value is a sentinel object used to "
"detect if some parameters are provided by the user. This sentinel is used "
"because ``None`` is a valid value for some parameters with a distinct "
"meaning. No code should directly use the :const:`MISSING` value."
msgstr ""
"如上所示,:const:`MISSING` 值是一个哨兵对象,用于检测一些形参是否由用户提供。使用它是因为 ``None`` "
"对于一些形参来说是有效的用户值。任何代码都不应该直接使用 :const:`MISSING` 值。"
#: ../../library/dataclasses.rst:229
msgid "The parameters to :func:`field` are:"
msgstr ":func:`field` 的形参有:"
#: ../../library/dataclasses.rst:231
msgid ""
"``default``: If provided, this will be the default value for this field. "
"This is needed because the :meth:`field` call itself replaces the normal "
"position of the default value."
msgstr ""
"``default``:如果提供,这将是该字段的默认值。设计这个形参是因为 :meth:`field` 调用将会占据原来用来提供默认值的位置。"
#: ../../library/dataclasses.rst:235
msgid ""
"``default_factory``: If provided, it must be a zero-argument callable that "
"will be called when a default value is needed for this field. Among other "
"purposes, this can be used to specify fields with mutable default values, as"
" discussed below. It is an error to specify both ``default`` and "
"``default_factory``."
msgstr ""
"``default_factory``:如果提供,它必须是一个需要零个参数的可调用对象,当该字段需要一个默认值时,它将被调用。这能解决当默认值是可变对象时会带来的问题,如下所述。同时指定"
" ``default`` 和 ``default_factory`` 将产生错误。"
#: ../../library/dataclasses.rst:241
msgid ""
"``init``: If true (the default), this field is included as a parameter to "
"the generated :meth:`__init__` method."
msgstr "``init`` :如果为true(默认值),则该字段作为参数包含在生成的 :meth:`__init__` 方法中。"
#: ../../library/dataclasses.rst:244
msgid ""
"``repr``: If true (the default), this field is included in the string "
"returned by the generated :meth:`__repr__` method."
msgstr "``repr`` :如果为true(默认值),则该字段包含在生成的 :meth:`__repr__` 方法返回的字符串中。"
#: ../../library/dataclasses.rst:247
msgid ""
"``hash``: This can be a bool or ``None``. If true, this field is included "
"in the generated :meth:`__hash__` method. If ``None`` (the default), use "
"the value of ``compare``: this would normally be the expected behavior. A "
"field should be considered in the hash if it's used for comparisons. "
"Setting this value to anything other than ``None`` is discouraged."
msgstr ""
"``hash`` :这可以是布尔值或 ``None`` 。如果为true,则此字段包含在生成的 :meth:`__hash__` 方法中。如果为 "
"``None`` (默认值),请使用 ``compare`` 的值,这通常是预期的行为。如果字段用于比较,则应在 hash "
"中考虑该字段。不鼓励将此值设置为 ``None`` 以外的任何值。"
#: ../../library/dataclasses.rst:254
msgid ""
"One possible reason to set ``hash=False`` but ``compare=True`` would be if a"
" field is expensive to compute a hash value for, that field is needed for "
"equality testing, and there are other fields that contribute to the type's "
"hash value. Even if a field is excluded from the hash, it will still be "
"used for comparisons."
msgstr ""
"设置 ``hash=False`` 但 ``compare=True`` "
"的一个合理情况是,一个计算哈希值的代价很高的字段是检验等价性需要的,且还有其他字段可以用于计算类型的哈希值。可以从哈希值中排除该字段,但仍令它用于比较。"
#: ../../library/dataclasses.rst:260
msgid ""
"``compare``: If true (the default), this field is included in the generated "
"equality and comparison methods (:meth:`__eq__`, :meth:`__gt__`, et al.)."
msgstr ""
"``compare`` :如果为true(默认值),则该字段包含在生成的相等性和比较方法中( :meth:`__eq__` , "
":meth:`__gt__` 等等)。"
#: ../../library/dataclasses.rst:264
msgid ""
"``metadata``: This can be a mapping or None. None is treated as an empty "
"dict. This value is wrapped in :func:`~types.MappingProxyType` to make it "
"read-only, and exposed on the :class:`Field` object. It is not used at all "
"by Data Classes, and is provided as a third-party extension mechanism. "
"Multiple third-parties can each have their own key, to use as a namespace in"
" the metadata."
msgstr ""
"``metadata``:可以是映射或 None。None 被视为一个空的字典。这个值将被包装在 "
":func:`~types.MappingProxyType` 中,使其只读,并暴露在 :class:`Field` "
"对象上。数据类不使用它——它是作为第三方扩展机制提供的。多个第三方可以各自拥有自己的键,以用作元数据中的命名空间。"
#: ../../library/dataclasses.rst:272
msgid ""
"``kw_only``: If true, this field will be marked as keyword-only. This is "
"used when the generated :meth:`__init__` method's parameters are computed."
msgstr ""
"``kw_only``: 如果为真值,则此字段将被标记为仅限关键字。 这将在当计算出所生成的 :meth:`__init__` 方法的形参时被使用。"
#: ../../library/dataclasses.rst:278
msgid ""
"If the default value of a field is specified by a call to :func:`field()`, "
"then the class attribute for this field will be replaced by the specified "
"``default`` value. If no ``default`` is provided, then the class attribute "
"will be deleted. The intent is that after the :func:`dataclass` decorator "
"runs, the class attributes will all contain the default values for the "
"fields, just as if the default value itself were specified. For example, "
"after::"
msgstr ""
"如果通过调用 :func:`field()` 指定字段的默认值,则该字段对应的类属性的值将最终被替换为指定的 ``default`` 值。如果没有提供 "
"``default``,那么将删除该字段对应的类属性。目的是在 :func:`dataclass` "
"装饰器运行之后,类属性将包含字段的默认值,和直接指定了默认值一样。例如,在运行如下代码之后:"
#: ../../library/dataclasses.rst:294
msgid ""
"The class attribute ``C.z`` will be ``10``, the class attribute ``C.t`` will"
" be ``20``, and the class attributes ``C.x`` and ``C.y`` will not be set."
msgstr ""
"类属性 ``C.z`` 将是 ``10``,类属性 ``C.t`` 将是 ``20``,类属性 ``C.x`` 和 ``C.y`` 将不设置。"
#: ../../library/dataclasses.rst:300
msgid ""
":class:`Field` objects describe each defined field. These objects are "
"created internally, and are returned by the :func:`fields` module-level "
"method (see below). Users should never instantiate a :class:`Field` object "
"directly. Its documented attributes are:"
msgstr ""
":class:`Field` 对象描述每个已定义的字段。这些对象在内部被创建,并由 :func:`fields` "
"模块级方法返回(见下)。用户永远不应该直接实例化 :class:`Field` 对象。它的下列属性的含义是由文档规定的:"
#: ../../library/dataclasses.rst:305
msgid "``name``: The name of the field."
msgstr "``name``:字段的名称。"
#: ../../library/dataclasses.rst:307
msgid "``type``: The type of the field."
msgstr "``type``:字段的类型。"
#: ../../library/dataclasses.rst:309
msgid ""
"``default``, ``default_factory``, ``init``, ``repr``, ``hash``, ``compare``,"
" ``metadata``, and ``kw_only`` have the identical meaning and values as they"
" do in the :func:`field` function."
msgstr ""
"``default``, ``default_factory``, ``init``, ``repr``, ``hash``, ``compare``,"
" ``metadata`` 和 ``kw_only`` 具有与 :func:`field` 函数中对应参数相同的含义和值。"
#: ../../library/dataclasses.rst:313
msgid ""
"Other attributes may exist, but they are private and must not be inspected "
"or relied on."
msgstr "可能存在其他属性,但它们是私有的。用户不应检查或依赖于这些属性。"
#: ../../library/dataclasses.rst:318
msgid ""
"Returns a tuple of :class:`Field` objects that define the fields for this "
"dataclass. Accepts either a dataclass, or an instance of a dataclass. "
"Raises :exc:`TypeError` if not passed a dataclass or instance of one. Does "
"not return pseudo-fields which are ``ClassVar`` or ``InitVar``."
msgstr ""
"返回一个能描述此数据类所包含的字段的元组,元组的每一项都是 :class:`Field` "
"对象。接受数据类或数据类的实例。如果没有传递一个数据类或实例将引发 :exc:`TypeError`。不返回 ``ClassVar`` 或 "
"``InitVar`` 等伪字段。"
#: ../../library/dataclasses.rst:325
msgid ""
"Converts the dataclass ``obj`` to a dict (by using the factory function "
"``dict_factory``). Each dataclass is converted to a dict of its fields, as "
"``name: value`` pairs. dataclasses, dicts, lists, and tuples are recursed "
"into. Other objects are copied with :func:`copy.deepcopy`."
msgstr ""
"将数据类 ``obj`` 转换为一个字典(使用工厂函数 ``dict_factory``)。每个数据类被转换为以 ``name: value`` "
"键值对来储存其字段的字典。数据类、字典、列表和元组的内容会被递归地访问。其它对象用 :func:`copy.deepcopy` 来复制。"
#: ../../library/dataclasses.rst:331
msgid "Example of using :func:`asdict` on nested dataclasses::"
msgstr "在嵌套的数据类上使用 :func:`asdict` 的例子:"
#: ../../library/dataclasses.rst:348 ../../library/dataclasses.rst:368
msgid "To create a shallow copy, the following workaround may be used::"
msgstr "要创建一个浅拷贝,可以使用以下的变通方法:"
#: ../../library/dataclasses.rst:352
msgid ""
":func:`asdict` raises :exc:`TypeError` if ``obj`` is not a dataclass "
"instance."
msgstr "如果 ``obj`` 不是一个数据类实例,:func:`asdict` 引发 :exc:`TypeError`。"
#: ../../library/dataclasses.rst:357
msgid ""
"Converts the dataclass ``obj`` to a tuple (by using the factory function "
"``tuple_factory``). Each dataclass is converted to a tuple of its field "
"values. dataclasses, dicts, lists, and tuples are recursed into. Other "
"objects are copied with :func:`copy.deepcopy`."
msgstr ""
"将数据类 ``obj`` 转换为一个元组(使用工厂函数 "
"``tuple_factory``)。每个数据类被转换为其字段的值的元组。数据类、字典、列表和元组的内容会被递归地访问。其它对象用 "
":func:`copy.deepcopy` 来复制。"
#: ../../library/dataclasses.rst:363
msgid "Continuing from the previous example::"
msgstr "继续前一个例子:"
#: ../../library/dataclasses.rst:372
msgid ""
":func:`astuple` raises :exc:`TypeError` if ``obj`` is not a dataclass "
"instance."
msgstr "如果 ``obj`` 不是一个数据类实例,:func:`astuple` 引发 :exc:`TypeError`。"
#: ../../library/dataclasses.rst:377
msgid ""
"Creates a new dataclass with name ``cls_name``, fields as defined in "
"``fields``, base classes as given in ``bases``, and initialized with a "
"namespace as given in ``namespace``. ``fields`` is an iterable whose "
"elements are each either ``name``, ``(name, type)``, or ``(name, type, "
"Field)``. If just ``name`` is supplied, ``typing.Any`` is used for "
"``type``. The values of ``init``, ``repr``, ``eq``, ``order``, "
"``unsafe_hash``, ``frozen``, ``match_args``, ``kw_only``, and ``slots`` "
"have the same meaning as they do in :func:`dataclass`."
msgstr ""
"新建一个名为 ``cls_name`` 的数据类,其字段在 ``fields`` 中定义,基类在 ``bases`` 中给出,并使用在 "
"``namespace`` 中给出的命名空间进行初始化。 ``fields`` 是一个可迭代对象,其中的每个元素均为 ``name``, "
"``(name, type)`` 或 ``(name, type, Field)``。 如果只提供了 ``name``,则 ``typing.Any``"
" 会被用作 ``type``。 ``init``, ``repr``, ``eq``, ``order``, ``unsafe_hash``, "
"``frozen``, ``match_args``, ``kw_only`` 和 ``slots`` 等值与它们在 "
":func:`dataclass` 中的含义相同。"
#: ../../library/dataclasses.rst:387
msgid ""
"This function is not strictly required, because any Python mechanism for "
"creating a new class with ``__annotations__`` can then apply the "
":func:`dataclass` function to convert that class to a dataclass. This "
"function is provided as a convenience. For example::"
msgstr ""
"此函数不是必需的,因为任何用于创建带有 ``__annotations__`` 的新类的 Python 机制都可以进一步用 "
":func:`dataclass` 函数将创建的类转换为数据类。提供此函数是为了方便。例如:"
#: ../../library/dataclasses.rst:399
msgid "Is equivalent to::"
msgstr "等价于:"
#: ../../library/dataclasses.rst:412
msgid ""
"Creates a new object of the same type as ``obj``, replacing fields with "
"values from ``changes``. If ``obj`` is not a Data Class, raises "
":exc:`TypeError`. If values in ``changes`` do not specify fields, raises "
":exc:`TypeError`."
msgstr ""
"创建一个与 ``obj`` 类型相同的新对象,将字段替换为 ``changes`` 里的值。如果 ``obj`` 不是数据类,则抛出 "
":exc:`TypeError` 。如果 ``changes`` 里的值没有指定要替换的字段名,则抛出 :exc:`TypeError`。"
#: ../../library/dataclasses.rst:417
msgid ""
"The newly returned object is created by calling the :meth:`__init__` method "
"of the dataclass. This ensures that :meth:`__post_init__`, if present, is "
"also called."
msgstr ""
"新返回的对象通过调用数据类的 :meth:`__init__` 方法创建。这确保了如果存在 :meth:`__post_init__` ,其也被调用。"
#: ../../library/dataclasses.rst:421
msgid ""
"Init-only variables without default values, if any exist, must be specified "
"on the call to :func:`replace` so that they can be passed to "
":meth:`__init__` and :meth:`__post_init__`."
msgstr ""
"如果存在没有默认值的仅初始化变量,必须在调用 :func:`replace` 时指定,以便它们可以传递给 :meth:`__init__` 和 "
":meth:`__post_init__` 。"
#: ../../library/dataclasses.rst:425
msgid ""
"It is an error for ``changes`` to contain any fields that are defined as "
"having ``init=False``. A :exc:`ValueError` will be raised in this case."
msgstr ""
"``changes`` 包含任何定义为 ``init=False`` 的字段是错误的。在这种情况下会引发 :exc:`ValueError` 。"
#: ../../library/dataclasses.rst:429
msgid ""
"Be forewarned about how ``init=False`` fields work during a call to "
":func:`replace`. They are not copied from the source object, but rather are"
" initialized in :meth:`__post_init__`, if they're initialized at all. It is"
" expected that ``init=False`` fields will be rarely and judiciously used. "
"If they are used, it might be wise to have alternate class constructors, or "
"perhaps a custom ``replace()`` (or similarly named) method which handles "
"instance copying."
msgstr ""
"提前提醒, 字段在 :func:`replace` 被调用时的行为是,当它们被初始化时,不是从源对象直接复制,而是在 "
":meth:`__post_init__` 中初始化。除非保持审慎,否则 ``init=False`` 字段大概很少能被正确地使用。如果有使用 "
"``init=False`` 的字段,那么使用另外的类构造器,或自定义 ``replace()`` 方法(或类似名称的方法)来复制实例,可能会更好。"
#: ../../library/dataclasses.rst:440
msgid ""
"Return ``True`` if its parameter is a dataclass or an instance of one, "
"otherwise return ``False``."
msgstr "如果其形参为数据类,或其实例,返回 ``True``,否则返回 ``False``。"
#: ../../library/dataclasses.rst:443
msgid ""
"If you need to know if a class is an instance of a dataclass (and not a "
"dataclass itself), then add a further check for ``not isinstance(obj, "
"type)``::"
msgstr ""
"如果你需要知道一个类是否是一个数据类的实例(而不是一个数据类本身),那么再添加一个 ``not isinstance(obj, type)`` 检查:"
#: ../../library/dataclasses.rst:452
msgid "A sentinel value signifying a missing default or default_factory."
msgstr "一个指明“没有提供 default 或 default_factory”的监视值。"
#: ../../library/dataclasses.rst:456
msgid ""
"A sentinel value used as a type annotation. Any fields after a pseudo-field"
" with the type of :const:`KW_ONLY` are marked as keyword-only fields. Note "
"that a pseudo-field of type :const:`KW_ONLY` is otherwise completely "
"ignored. This includes the name of such a field. By convention, a name of "
"``_`` is used for a :const:`KW_ONLY` field. Keyword-only fields signify "
":meth:`__init__` parameters that must be specified as keywords when the "
"class is instantiated."
msgstr ""
"一个用作类型标注的监视值。 任何在伪字段之后的类型为 :const:`KW_ONLY` 的字段会被标记为仅限关键字字段。 请注意在其他情况下 "
":const:`KW_ONLY` 类型的伪字段会被完全忽略。 这包括此类字段的名称。 根据惯例,名称 ``_`` 会被用作 "
":const:`KW_ONLY` 字段。 仅限关键字字段指明当类被实例化时 :meth:`__init__` 形参必须以关键字形式来指定。"
#: ../../library/dataclasses.rst:465
msgid ""
"In this example, the fields ``y`` and ``z`` will be marked as keyword-only "
"fields::"
msgstr "在这个例子中,字段 ``y`` 和 ``z`` 将被标记为仅限关键字字段::"
#: ../../library/dataclasses.rst:476
msgid ""
"In a single dataclass, it is an error to specify more than one field whose "
"type is :const:`KW_ONLY`."
msgstr "在单个数据类中,指定一个以上 :const:`KW_ONLY` 类型的字段将导致错误。"
#: ../../library/dataclasses.rst:483
msgid ""
"Raised when an implicitly defined :meth:`__setattr__` or :meth:`__delattr__`"
" is called on a dataclass which was defined with ``frozen=True``. It is a "
"subclass of :exc:`AttributeError`."
msgstr ""
"在使用 ``frozen=True`` 定义的数据类上调用隐式定义的 :meth:`__setattr__` 或 :meth:`__delattr__`"
" 时引发。 这是 :exc:`AttributeError` 的一个子类。"
#: ../../library/dataclasses.rst:488
msgid "Post-init processing"
msgstr "初始化后处理"
#: ../../library/dataclasses.rst:490
msgid ""
"The generated :meth:`__init__` code will call a method named "
":meth:`__post_init__`, if :meth:`__post_init__` is defined on the class. It"
" will normally be called as ``self.__post_init__()``. However, if any "
"``InitVar`` fields are defined, they will also be passed to "
":meth:`__post_init__` in the order they were defined in the class. If no "
":meth:`__init__` method is generated, then :meth:`__post_init__` will not "
"automatically be called."
msgstr ""
"生成的 :meth:`__init__` 代码将调用一个名为 :meth:`__post_init__` 的方法,如果在类上已经定义了 "
":meth:`__post_init__` 。它通常被称为 ``self.__post_init__()`` 。但是,如果定义了任何 "
"``InitVar`` 字段,它们也将按照它们在类中定义的顺序传递给 :meth:`__post_init__` 。 如果没有 "
":meth:`__init__` 方法生成,那么 :meth:`__post_init__` 将不会被自动调用。"
#: ../../library/dataclasses.rst:498
msgid ""
"Among other uses, this allows for initializing field values that depend on "
"one or more other fields. For example::"
msgstr "在其他用途中,这允许初始化依赖于一个或多个其他字段的字段值。例如::"
#: ../../library/dataclasses.rst:510
msgid ""
"The :meth:`__init__` method generated by :func:`dataclass` does not call "
"base class :meth:`__init__` methods. If the base class has an "
":meth:`__init__` method that has to be called, it is common to call this "
"method in a :meth:`__post_init__` method::"
msgstr ""
"由 :func:`dataclass` 所生成的 :meth:`__init__` 方法不会调用基类的 :meth:`__init__` 方法。 "
"如果基类有需要被调用的 :meth:`__init__` 方法,通常是在 :meth:`__post_init__` 方法中调用此方法::"
#: ../../library/dataclasses.rst:527
msgid ""
"Note, however, that in general the dataclass-generated :meth:`__init__` "
"methods don't need to be called, since the derived dataclass will take care "
"of initializing all fields of any base class that is a dataclass itself."
msgstr ""
"但是请注意,一般来说 dataclass 生成的 :meth:`__init__` 方法不需要被调用,因为派生的 dataclass "
"将负责初始化任何自身为 dataclass 的基类的所有字段。"
#: ../../library/dataclasses.rst:531
msgid ""
"See the section below on init-only variables for ways to pass parameters to "
":meth:`__post_init__`. Also see the warning about how :func:`replace` "
"handles ``init=False`` fields."
msgstr ""
"有关将参数传递给 :meth:`__post_init__` 的方法,请参阅下面有关仅初始化变量的段落。另请参阅关于 :func:`replace` "
"处理 ``init=False`` 字段的警告。"
#: ../../library/dataclasses.rst:536
msgid "Class variables"
msgstr "类变量"
#: ../../library/dataclasses.rst:538
msgid ""
"One of the few places where :func:`dataclass` actually inspects the type of "
"a field is to determine if a field is a class variable as defined in "
":pep:`526`. It does this by checking if the type of the field is "
"``typing.ClassVar``. If a field is a ``ClassVar``, it is excluded from "
"consideration as a field and is ignored by the dataclass mechanisms. Such "
"``ClassVar`` pseudo-fields are not returned by the module-level "
":func:`fields` function."
msgstr ""
"在 :func:`dataclass` 会实际检查字段类型的少数几个地方之一是确定字符是否为如 :pep:`526` 所定义的类变量。 "
"它通过检查字段的类型是否为 ``typing.ClassVar`` 来实现这一点。 如果一个字段是 "
"``ClassVar``,它将被排除在考虑范围之外并被数据类机制所忽略。 这样的 ``ClassVar`` 伪字段将不会被模块层级的 "
":func:`fields` 函数返回。"
#: ../../library/dataclasses.rst:547
msgid "Init-only variables"
msgstr "仅初始化变量"
#: ../../library/dataclasses.rst:549
msgid ""
"Another place where :func:`dataclass` inspects a type annotation is to "
"determine if a field is an init-only variable. It does this by seeing if "
"the type of a field is of type ``dataclasses.InitVar``. If a field is an "
"``InitVar``, it is considered a pseudo-field called an init-only field. As "
"it is not a true field, it is not returned by the module-level "
":func:`fields` function. Init-only fields are added as parameters to the "
"generated :meth:`__init__` method, and are passed to the optional "
":meth:`__post_init__` method. They are not otherwise used by dataclasses."
msgstr ""
#: ../../library/dataclasses.rst:559
msgid ""
"For example, suppose a field will be initialized from a database, if a value"
" is not provided when creating the class::"
msgstr "例如,假设在创建类时没有为某个字段提供值,初始化时将从数据库中取值::"
#: ../../library/dataclasses.rst:574
msgid ""
"In this case, :func:`fields` will return :class:`Field` objects for ``i`` "
"and ``j``, but not for ``database``."
msgstr ""
"在这种情况下, :func:`fields` 将返回 ``i`` 和 ``j`` 的 :class:`Field` 对象,但不包括 "
"``database`` 。"
#: ../../library/dataclasses.rst:578
msgid "Frozen instances"
msgstr "冻结的实例"
#: ../../library/dataclasses.rst:580
msgid ""
"It is not possible to create truly immutable Python objects. However, by "
"passing ``frozen=True`` to the :meth:`dataclass` decorator you can emulate "
"immutability. In that case, dataclasses will add :meth:`__setattr__` and "
":meth:`__delattr__` methods to the class. These methods will raise a "
":exc:`FrozenInstanceError` when invoked."
msgstr ""
"无法创建真正不可变的 Python 对象。但是,通过将 ``frozen=True`` 传递给 :meth:`dataclass` "
"装饰器,你可以模拟不变性。在这种情况下,数据类将向类添加 :meth:`__setattr__` 和 :meth:`__delattr__` 方法。 "
"些方法在调用时会引发 :exc:`FrozenInstanceError` 。"
#: ../../library/dataclasses.rst:586
msgid ""
"There is a tiny performance penalty when using ``frozen=True``: "
":meth:`__init__` cannot use simple assignment to initialize fields, and must"
" use :meth:`object.__setattr__`."
msgstr ""
"使用 ``frozen=True`` 时会有很小的性能损失: :meth:`__ init__` 不能使用简单的赋值来初始化字段,并必须使用 "
":meth:`object.__setattr__`。"
#: ../../library/dataclasses.rst:591
msgid "Inheritance"
msgstr "继承"
#: ../../library/dataclasses.rst:593
msgid ""
"When the dataclass is being created by the :meth:`dataclass` decorator, it "
"looks through all of the class's base classes in reverse MRO (that is, "
"starting at :class:`object`) and, for each dataclass that it finds, adds the"
" fields from that base class to an ordered mapping of fields. After all of "
"the base class fields are added, it adds its own fields to the ordered "
"mapping. All of the generated methods will use this combined, calculated "
"ordered mapping of fields. Because the fields are in insertion order, "
"derived classes override base classes. An example::"
msgstr ""
"当数据类由 :meth:`dataclass` 装饰器创建时,它会按反向 MRO 顺序(即,从 :class:`object` "
"开始)查看它的所有基类,并且将找到的每个数据类的字段添加到一个有序映射中。添加完所有基类字段后,它会将自己的字段添加到这个有序映射中。所有生成的方法都将使用这个有序映射。字段会遵守它们被插入的顺序,因此派生类会重写基类。一个例子:"
#: ../../library/dataclasses.rst:613
msgid ""
"The final list of fields is, in order, ``x``, ``y``, ``z``. The final type "
"of ``x`` is ``int``, as specified in class ``C``."
msgstr ""
"最后的字段列表依次是 ``x`` 、 ``y`` 、 ``z`` 。 ``x`` 的最终类型是 ``int`` ,如类 ``C`` 中所指定的那样。"
#: ../../library/dataclasses.rst:616
msgid "The generated :meth:`__init__` method for ``C`` will look like::"
msgstr "为 ``C`` 生成的 :meth:`__init__` 方法看起来像::"
#: ../../library/dataclasses.rst:621
msgid "Re-ordering of keyword-only parameters in :meth:`__init__`"
msgstr ":meth:`__init__` 中仅限关键字字段的重新排序"
#: ../../library/dataclasses.rst:623
msgid ""
"After the parameters needed for :meth:`__init__` are computed, any keyword-"
"only parameters are moved to come after all regular (non-keyword-only) "
"parameters. This is a requirement of how keyword-only parameters are "
"implemented in Python: they must come after non-keyword-only parameters."
msgstr ""
"在计算出 :meth:`__init__` 所需要的形参之后,任何仅限关键字形参会被移至所有常规(非仅限关键字)形参的后面。 这是 Python "
"中实现仅限关键字形参所要求的:它们必须位于非仅限关键字形参之后。"
#: ../../library/dataclasses.rst:629
msgid ""
"In this example, ``Base.y``, ``Base.w``, and ``D.t`` are keyword-only "
"fields, and ``Base.x`` and ``D.z`` are regular fields::"
msgstr ""
"在这个例子中,``Base.y``, ``Base.w``, and ``D.t`` 是仅限关键字字段,而 ``Base.x`` 和 ``D.z`` "
"是常规字段::"
#: ../../library/dataclasses.rst:644
msgid "The generated :meth:`__init__` method for ``D`` will look like::"
msgstr "为 ``D`` 生成的 :meth:`__init__` 方法看起来像是这样::"
#: ../../library/dataclasses.rst:648
msgid ""
"Note that the parameters have been re-ordered from how they appear in the "
"list of fields: parameters derived from regular fields are followed by "
"parameters derived from keyword-only fields."
msgstr "请注意形参原来在字段列表中出现的位置已被重新排序:前面是来自常规字段的形参而后面是来自仅限关键字字段的形参。"
#: ../../library/dataclasses.rst:652
msgid ""
"The relative ordering of keyword-only parameters is maintained in the re-"
"ordered :meth:`__init__` parameter list."
msgstr "仅限关键字形参的相对顺序会在重新排序的 :meth:`__init__` 形参列表中保持原样。"
#: ../../library/dataclasses.rst:657
msgid "Default factory functions"
msgstr "默认工厂函数"
#: ../../library/dataclasses.rst:659
msgid ""
"If a :func:`field` specifies a ``default_factory``, it is called with zero "
"arguments when a default value for the field is needed. For example, to "
"create a new instance of a list, use::"
msgstr ""
"如果一个 :func:`field` 指定了一个 ``default_factory`` "
",当需要该字段的默认值时,将使用零参数调用它。例如,要创建列表的新实例,请使用::"
#: ../../library/dataclasses.rst:665
msgid ""
"If a field is excluded from :meth:`__init__` (using ``init=False``) and the "
"field also specifies ``default_factory``, then the default factory function "
"will always be called from the generated :meth:`__init__` function. This "
"happens because there is no other way to give the field an initial value."
msgstr ""
"如果一个字段被排除在 :meth:`__init__` 之外(使用 ``init=False`` )并且字段也指定 "
"``default_factory`` ,则默认的工厂函数将始终从生成的 :meth:`__init__` "
"函数调用。发生这种情况是因为没有其他方法可以为字段提供初始值。"
#: ../../library/dataclasses.rst:672
msgid "Mutable default values"
msgstr "可变的默认值"
#: ../../library/dataclasses.rst:674
msgid ""
"Python stores default member variable values in class attributes. Consider "
"this example, not using dataclasses::"
msgstr "Python 在类属性中存储默认成员变量值。思考这个例子,不使用数据类::"
#: ../../library/dataclasses.rst:689
msgid ""
"Note that the two instances of class ``C`` share the same class variable "
"``x``, as expected."
msgstr "请注意,类 ``C`` 的两个实例共享相同的类变量 ``x`` ,如预期的那样。"
#: ../../library/dataclasses.rst:692
msgid "Using dataclasses, *if* this code was valid::"
msgstr "使用数据类,*如果* 此代码有效:"
#: ../../library/dataclasses.rst:700
msgid "it would generate code similar to::"
msgstr "它生成的代码类似于::"
#: ../../library/dataclasses.rst:711
msgid ""
"This has the same issue as the original example using class ``C``. That is, "
"two instances of class ``D`` that do not specify a value for ``x`` when "
"creating a class instance will share the same copy of ``x``. Because "
"dataclasses just use normal Python class creation they also share this "
"behavior. There is no general way for Data Classes to detect this "
"condition. Instead, the :func:`dataclass` decorator will raise a "
":exc:`TypeError` if it detects a default parameter of type ``list``, "
"``dict``, or ``set``. This is a partial solution, but it does protect "
"against many common errors."
msgstr ""
"这与使用 ``C`` 类的原始示例具有相同的问题。 也就是说,当在创建类实例的时候 ``D`` 类的两个实例没有为 ``x`` 指定值则将共享同一个 "
"``x`` 的副本。 因为数据类只是使用普通的 Python 类创建方式所以它们也会共享此行为。 数据类没有任何通用方式来检测这种情况。 "
"相反地,:func:`dataclass` 装饰器在检测到类型为 ``list``, ``dict`` 或 ``set`` 的默认形参时将会引发 "
":exc:`TypeError`。 这是一个部分解决方案,但它确实能防止许多常见错误。"
#: ../../library/dataclasses.rst:722
msgid ""
"Using default factory functions is a way to create new instances of mutable "
"types as default values for fields::"
msgstr "使用默认工厂函数是一种创建可变类型新实例的方法,并将其作为字段的默认值::"
#: ../../library/dataclasses.rst:732
msgid "Descriptor-typed fields"
msgstr "描述器类型的字段"
#: ../../library/dataclasses.rst:734
msgid ""
"Fields that are assigned :ref:`descriptor objects <descriptors>` as their "
"default value have the following special behaviors:"
msgstr "当字段被 :ref:`描述器对象 <descriptors>` 赋值为默认值时会遵循以下行为:"
#: ../../library/dataclasses.rst:737
msgid ""
"The value for the field passed to the dataclass's ``__init__`` method is "
"passed to the descriptor's ``__set__`` method rather than overwriting the "
"descriptor object."
msgstr "字段的值被传递到数据类的 ``__init__`` 方法时,会传递给描述器的 ``__set__`` 方法,而不会覆盖掉描述器对象。"
#: ../../library/dataclasses.rst:740
msgid ""
"Similarly, when getting or setting the field, the descriptor's ``__get__`` "
"or ``__set__`` method is called rather than returning or overwriting the "
"descriptor object."
msgstr ""
"相似的是,当我们获取或设置字段的值时,不会覆盖或返回描述器对象,而是会调用描述器的 ``__get__`` 或 ``__set__`` 方法后返回。"
#: ../../library/dataclasses.rst:743
msgid ""
"To determine whether a field contains a default value, ``dataclasses`` will "
"call the descriptor's ``__get__`` method using its class access form (i.e. "
"``descriptor.__get__(obj=None, type=cls)``. If the descriptor returns a "
"value in this case, it will be used as the field's default. On the other "
"hand, if the descriptor raises :exc:`AttributeError` in this situation, no "
"default value will be provided for the field."
msgstr ""
"检测一个字段是否存在默认值时 ,``dataclasses`` 将使用类方法(即 ``descriptor.__get__(obj=None, "
"type=cls)`` )调用描述器的 ``__get__`` 方法。 在这种情况下,如果描述器拥有返回值 "
",则返回值为该字段的默认值。如果在这种情况下描述器抛出 :exc:`AttributeError` 错误,则该字段不存在默认值。"
#: ../../library/dataclasses.rst:778
msgid ""
"Note that if a field is annotated with a descriptor type, but is not "
"assigned a descriptor object as its default value, the field will act like a"
" normal field."
msgstr "若一个字段的类型是描述器,但其默认值并不是描述器对象,那么该字段只会像普通的字段一样工作。"