-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy path2.2.po
1952 lines (1736 loc) · 72.1 KB
/
2.2.po
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2001-2024, Python Software Foundation
# This file is distributed under the same license as the Python package.
#
# Translators:
msgid ""
msgstr ""
"Project-Id-Version: Python 3.13\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-27 00:14+0000\n"
"PO-Revision-Date: 2018-05-23 16:19+0000\n"
"Last-Translator: Adrian Liaw <[email protected]>\n"
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
"tw)\n"
"Language: zh_TW\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
#: ../../whatsnew/2.2.rst:3
msgid "What's New in Python 2.2"
msgstr "Python 2.2 有什麼新功能"
#: ../../whatsnew/2.2.rst:0
msgid "Author"
msgstr "作者"
#: ../../whatsnew/2.2.rst:5
msgid "A.M. Kuchling"
msgstr "A.M. Kuchling"
#: ../../whatsnew/2.2.rst:13
msgid "Introduction"
msgstr "簡介"
#: ../../whatsnew/2.2.rst:15
msgid ""
"This article explains the new features in Python 2.2.2, released on October "
"14, 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally "
"released on December 21, 2001."
msgstr ""
#: ../../whatsnew/2.2.rst:19
msgid ""
"Python 2.2 can be thought of as the \"cleanup release\". There are some "
"features such as generators and iterators that are completely new, but most "
"of the changes, significant and far-reaching though they may be, are aimed "
"at cleaning up irregularities and dark corners of the language design."
msgstr ""
#: ../../whatsnew/2.2.rst:24
msgid ""
"This article doesn't attempt to provide a complete specification of the new "
"features, but instead provides a convenient overview. For full details, you "
"should refer to the documentation for Python 2.2, such as the `Python "
"Library Reference <https://docs.python.org/2.2/lib/lib.html>`_ and the "
"`Python Reference Manual <https://docs.python.org/2.2/ref/ref.html>`_. If "
"you want to understand the complete implementation and design rationale for "
"a change, refer to the PEP for a particular new feature."
msgstr ""
#: ../../whatsnew/2.2.rst:43
msgid "PEPs 252 and 253: Type and Class Changes"
msgstr ""
#: ../../whatsnew/2.2.rst:45
msgid ""
"The largest and most far-reaching changes in Python 2.2 are to Python's "
"model of objects and classes. The changes should be backward compatible, so "
"it's likely that your code will continue to run unchanged, but the changes "
"provide some amazing new capabilities. Before beginning this, the longest "
"and most complicated section of this article, I'll provide an overview of "
"the changes and offer some comments."
msgstr ""
#: ../../whatsnew/2.2.rst:52
msgid ""
"A long time ago I wrote a web page listing flaws in Python's design. One of "
"the most significant flaws was that it's impossible to subclass Python types "
"implemented in C. In particular, it's not possible to subclass built-in "
"types, so you can't just subclass, say, lists in order to add a single "
"useful method to them. The :mod:`!UserList` module provides a class that "
"supports all of the methods of lists and that can be subclassed further, but "
"there's lots of C code that expects a regular Python list and won't accept "
"a :class:`~collections.UserList` instance."
msgstr ""
#: ../../whatsnew/2.2.rst:61
msgid ""
"Python 2.2 fixes this, and in the process adds some exciting new "
"capabilities. A brief summary:"
msgstr ""
#: ../../whatsnew/2.2.rst:64
msgid ""
"You can subclass built-in types such as lists and even integers, and your "
"subclasses should work in every place that requires the original type."
msgstr ""
#: ../../whatsnew/2.2.rst:67
msgid ""
"It's now possible to define static and class methods, in addition to the "
"instance methods available in previous versions of Python."
msgstr ""
#: ../../whatsnew/2.2.rst:70
msgid ""
"It's also possible to automatically call methods on accessing or setting an "
"instance attribute by using a new mechanism called :dfn:`properties`. Many "
"uses of :meth:`~object.__getattr__` can be rewritten to use properties "
"instead, making the resulting code simpler and faster. As a small side "
"benefit, attributes can now have docstrings, too."
msgstr ""
#: ../../whatsnew/2.2.rst:76
msgid ""
"The list of legal attributes for an instance can be limited to a particular "
"set using :dfn:`slots`, making it possible to safeguard against typos and "
"perhaps make more optimizations possible in future versions of Python."
msgstr ""
#: ../../whatsnew/2.2.rst:80
msgid ""
"Some users have voiced concern about all these changes. Sure, they say, the "
"new features are neat and lend themselves to all sorts of tricks that "
"weren't possible in previous versions of Python, but they also make the "
"language more complicated. Some people have said that they've always "
"recommended Python for its simplicity, and feel that its simplicity is being "
"lost."
msgstr ""
#: ../../whatsnew/2.2.rst:86
msgid ""
"Personally, I think there's no need to worry. Many of the new features are "
"quite esoteric, and you can write a lot of Python code without ever needed "
"to be aware of them. Writing a simple class is no more difficult than it "
"ever was, so you don't need to bother learning or teaching them unless "
"they're actually needed. Some very complicated tasks that were previously "
"only possible from C will now be possible in pure Python, and to my mind "
"that's all for the better."
msgstr ""
#: ../../whatsnew/2.2.rst:93
msgid ""
"I'm not going to attempt to cover every single corner case and small change "
"that were required to make the new features work. Instead this section will "
"paint only the broad strokes. See section :ref:`sect-rellinks`, \"Related "
"Links\", for further sources of information about Python 2.2's new object "
"model."
msgstr ""
#: ../../whatsnew/2.2.rst:100
msgid "Old and New Classes"
msgstr "舊的和新的類別"
#: ../../whatsnew/2.2.rst:102
msgid ""
"First, you should know that Python 2.2 really has two kinds of classes: "
"classic or old-style classes, and new-style classes. The old-style class "
"model is exactly the same as the class model in earlier versions of Python. "
"All the new features described in this section apply only to new-style "
"classes. This divergence isn't intended to last forever; eventually old-"
"style classes will be dropped, possibly in Python 3.0."
msgstr ""
#: ../../whatsnew/2.2.rst:109
msgid ""
"So how do you define a new-style class? You do it by subclassing an "
"existing new-style class. Most of Python's built-in types, such as "
"integers, lists, dictionaries, and even files, are new-style classes now. A "
"new-style class named :class:`object`, the base class for all built-in "
"types, has also been added so if no built-in type is suitable, you can just "
"subclass :class:`object`::"
msgstr ""
#: ../../whatsnew/2.2.rst:116
msgid ""
"class C(object):\n"
" def __init__ (self):\n"
" ...\n"
" ..."
msgstr ""
"class C(object):\n"
" def __init__ (self):\n"
" ...\n"
" ..."
#: ../../whatsnew/2.2.rst:121
msgid ""
"This means that :keyword:`class` statements that don't have any base classes "
"are always classic classes in Python 2.2. (Actually you can also change "
"this by setting a module-level variable named :attr:`!__metaclass__` --- "
"see :pep:`253` for the details --- but it's easier to just subclass :class:"
"`object`.)"
msgstr ""
#: ../../whatsnew/2.2.rst:126
msgid ""
"The type objects for the built-in types are available as built-ins, named "
"using a clever trick. Python has always had built-in functions named :func:"
"`int`, :func:`float`, and :func:`str`. In 2.2, they aren't functions any "
"more, but type objects that behave as factories when called. ::"
msgstr ""
#: ../../whatsnew/2.2.rst:131
msgid ""
">>> int\n"
"<type 'int'>\n"
">>> int('123')\n"
"123"
msgstr ""
">>> int\n"
"<type 'int'>\n"
">>> int('123')\n"
"123"
#: ../../whatsnew/2.2.rst:136
msgid ""
"To make the set of types complete, new type objects such as :func:`dict` "
"and :func:`!file` have been added. Here's a more interesting example, "
"adding a :meth:`!lock` method to file objects::"
msgstr ""
#: ../../whatsnew/2.2.rst:140
msgid ""
"class LockableFile(file):\n"
" def lock (self, operation, length=0, start=0, whence=0):\n"
" import fcntl\n"
" return fcntl.lockf(self.fileno(), operation,\n"
" length, start, whence)"
msgstr ""
"class LockableFile(file):\n"
" def lock (self, operation, length=0, start=0, whence=0):\n"
" import fcntl\n"
" return fcntl.lockf(self.fileno(), operation,\n"
" length, start, whence)"
#: ../../whatsnew/2.2.rst:146
msgid ""
"The now-obsolete :mod:`!posixfile` module contained a class that emulated "
"all of a file object's methods and also added a :meth:`!lock` method, but "
"this class couldn't be passed to internal functions that expected a built-in "
"file, something which is possible with our new :class:`!LockableFile`."
msgstr ""
#: ../../whatsnew/2.2.rst:153
msgid "Descriptors"
msgstr "描述器"
#: ../../whatsnew/2.2.rst:155
msgid ""
"In previous versions of Python, there was no consistent way to discover what "
"attributes and methods were supported by an object. There were some informal "
"conventions, such as defining :attr:`!__members__` and :attr:`!__methods__` "
"attributes that were lists of names, but often the author of an extension "
"type or a class wouldn't bother to define them. You could fall back on "
"inspecting the :attr:`~object.__dict__` of an object, but when class "
"inheritance or an arbitrary :meth:`!__getattr__` hook were in use this could "
"still be inaccurate."
msgstr ""
#: ../../whatsnew/2.2.rst:163
msgid ""
"The one big idea underlying the new class model is that an API for "
"describing the attributes of an object using :dfn:`descriptors` has been "
"formalized. Descriptors specify the value of an attribute, stating whether "
"it's a method or a field. With the descriptor API, static methods and class "
"methods become possible, as well as more exotic constructs."
msgstr ""
#: ../../whatsnew/2.2.rst:169
msgid ""
"Attribute descriptors are objects that live inside class objects, and have a "
"few attributes of their own:"
msgstr ""
#: ../../whatsnew/2.2.rst:172
msgid ":attr:`~definition.__name__` is the attribute's name."
msgstr ":attr:`~definition.__name__` 是屬性的名稱。"
#: ../../whatsnew/2.2.rst:174
msgid ":attr:`~definition.__doc__` is the attribute's docstring."
msgstr ":attr:`~definition.__doc__` 是屬性的文件字串 (docstring)。"
#: ../../whatsnew/2.2.rst:176
msgid ""
"``__get__(object)`` is a method that retrieves the attribute value from "
"*object*."
msgstr ""
#: ../../whatsnew/2.2.rst:179
msgid "``__set__(object, value)`` sets the attribute on *object* to *value*."
msgstr ""
#: ../../whatsnew/2.2.rst:181
msgid ""
"``__delete__(object, value)`` deletes the *value* attribute of *object*."
msgstr ""
#: ../../whatsnew/2.2.rst:183
msgid ""
"For example, when you write ``obj.x``, the steps that Python actually "
"performs are::"
msgstr ""
#: ../../whatsnew/2.2.rst:186
msgid ""
"descriptor = obj.__class__.x\n"
"descriptor.__get__(obj)"
msgstr ""
"descriptor = obj.__class__.x\n"
"descriptor.__get__(obj)"
#: ../../whatsnew/2.2.rst:189
msgid ""
"For methods, :meth:`descriptor.__get__ <object.__get__>` returns a temporary "
"object that's callable, and wraps up the instance and the method to be "
"called on it. This is also why static methods and class methods are now "
"possible; they have descriptors that wrap up just the method, or the method "
"and the class. As a brief explanation of these new kinds of methods, static "
"methods aren't passed the instance, and therefore resemble regular "
"functions. Class methods are passed the class of the object, but not the "
"object itself. Static and class methods are defined like this::"
msgstr ""
#: ../../whatsnew/2.2.rst:199
msgid ""
"class C(object):\n"
" def f(arg1, arg2):\n"
" ...\n"
" f = staticmethod(f)\n"
"\n"
" def g(cls, arg1, arg2):\n"
" ...\n"
" g = classmethod(g)"
msgstr ""
"class C(object):\n"
" def f(arg1, arg2):\n"
" ...\n"
" f = staticmethod(f)\n"
"\n"
" def g(cls, arg1, arg2):\n"
" ...\n"
" g = classmethod(g)"
#: ../../whatsnew/2.2.rst:208
msgid ""
"The :func:`staticmethod` function takes the function :func:`!f`, and returns "
"it wrapped up in a descriptor so it can be stored in the class object. You "
"might expect there to be special syntax for creating such methods (``def "
"static f``, ``defstatic f()``, or something like that) but no such syntax "
"has been defined yet; that's been left for future versions of Python."
msgstr ""
#: ../../whatsnew/2.2.rst:214
msgid ""
"More new features, such as slots and properties, are also implemented as new "
"kinds of descriptors, and it's not difficult to write a descriptor class "
"that does something novel. For example, it would be possible to write a "
"descriptor class that made it possible to write Eiffel-style preconditions "
"and postconditions for a method. A class that used this feature might be "
"defined like this::"
msgstr ""
#: ../../whatsnew/2.2.rst:221
msgid ""
"from eiffel import eiffelmethod\n"
"\n"
"class C(object):\n"
" def f(self, arg1, arg2):\n"
" # The actual function\n"
" ...\n"
" def pre_f(self):\n"
" # Check preconditions\n"
" ...\n"
" def post_f(self):\n"
" # Check postconditions\n"
" ...\n"
"\n"
" f = eiffelmethod(f, pre_f, post_f)"
msgstr ""
#: ../../whatsnew/2.2.rst:236
msgid ""
"Note that a person using the new :func:`!eiffelmethod` doesn't have to "
"understand anything about descriptors. This is why I think the new features "
"don't increase the basic complexity of the language. There will be a few "
"wizards who need to know about it in order to write :func:`!eiffelmethod` or "
"the ZODB or whatever, but most users will just write code on top of the "
"resulting libraries and ignore the implementation details."
msgstr ""
#: ../../whatsnew/2.2.rst:245
msgid "Multiple Inheritance: The Diamond Rule"
msgstr ""
#: ../../whatsnew/2.2.rst:247
msgid ""
"Multiple inheritance has also been made more useful through changing the "
"rules under which names are resolved. Consider this set of classes (diagram "
"taken from :pep:`253` by Guido van Rossum)::"
msgstr ""
#: ../../whatsnew/2.2.rst:251
msgid ""
" class A:\n"
" ^ ^ def save(self): ...\n"
" / \\\n"
" / \\\n"
" / \\\n"
" / \\\n"
"class B class C:\n"
" ^ ^ def save(self): ...\n"
" \\ /\n"
" \\ /\n"
" \\ /\n"
" \\ /\n"
" class D"
msgstr ""
" class A:\n"
" ^ ^ def save(self): ...\n"
" / \\\n"
" / \\\n"
" / \\\n"
" / \\\n"
"class B class C:\n"
" ^ ^ def save(self): ...\n"
" \\ /\n"
" \\ /\n"
" \\ /\n"
" \\ /\n"
" class D"
#: ../../whatsnew/2.2.rst:265
msgid ""
"The lookup rule for classic classes is simple but not very smart; the base "
"classes are searched depth-first, going from left to right. A reference to :"
"meth:`!D.save` will search the classes :class:`!D`, :class:`!B`, and then :"
"class:`!A`, where :meth:`!save` would be found and returned. :meth:`!C."
"save` would never be found at all. This is bad, because if :class:`!C`'s :"
"meth:`!save` method is saving some internal state specific to :class:`!C`, "
"not calling it will result in that state never getting saved."
msgstr ""
#: ../../whatsnew/2.2.rst:273
msgid ""
"New-style classes follow a different algorithm that's a bit more complicated "
"to explain, but does the right thing in this situation. (Note that Python "
"2.3 changes this algorithm to one that produces the same results in most "
"cases, but produces more useful results for really complicated inheritance "
"graphs.)"
msgstr ""
#: ../../whatsnew/2.2.rst:278
msgid ""
"List all the base classes, following the classic lookup rule and include a "
"class multiple times if it's visited repeatedly. In the above example, the "
"list of visited classes is [:class:`!D`, :class:`!B`, :class:`!A`, :class:`!"
"C`, :class:`!A`]."
msgstr ""
#: ../../whatsnew/2.2.rst:283
msgid ""
"Scan the list for duplicated classes. If any are found, remove all but one "
"occurrence, leaving the *last* one in the list. In the above example, the "
"list becomes [:class:`!D`, :class:`!B`, :class:`!C`, :class:`!A`] after "
"dropping duplicates."
msgstr ""
#: ../../whatsnew/2.2.rst:288
msgid ""
"Following this rule, referring to :meth:`!D.save` will return :meth:`!C."
"save`, which is the behaviour we're after. This lookup rule is the same as "
"the one followed by Common Lisp. A new built-in function, :func:`super`, "
"provides a way to get at a class's superclasses without having to "
"reimplement Python's algorithm. The most commonly used form will be "
"``super(class, obj)``, which returns a bound superclass object (not the "
"actual class object). This form will be used in methods to call a method in "
"the superclass; for example, :class:`!D`'s :meth:`!save` method would look "
"like this::"
msgstr ""
#: ../../whatsnew/2.2.rst:297
msgid ""
"class D (B,C):\n"
" def save (self):\n"
" # Call superclass .save()\n"
" super(D, self).save()\n"
" # Save D's private information here\n"
" ..."
msgstr ""
#: ../../whatsnew/2.2.rst:304
msgid ""
":func:`super` can also return unbound superclass objects when called as "
"``super(class)`` or ``super(class1, class2)``, but this probably won't often "
"be useful."
msgstr ""
#: ../../whatsnew/2.2.rst:310
msgid "Attribute Access"
msgstr ""
#: ../../whatsnew/2.2.rst:312
msgid ""
"A fair number of sophisticated Python classes define hooks for attribute "
"access using :meth:`~object.__getattr__`; most commonly this is done for "
"convenience, to make code more readable by automatically mapping an "
"attribute access such as ``obj.parent`` into a method call such as ``obj."
"get_parent``. Python 2.2 adds some new ways of controlling attribute access."
msgstr ""
#: ../../whatsnew/2.2.rst:318
msgid ""
"First, ``__getattr__(attr_name)`` is still supported by new-style classes, "
"and nothing about it has changed. As before, it will be called when an "
"attempt is made to access ``obj.foo`` and no attribute named ``foo`` is "
"found in the instance's dictionary."
msgstr ""
#: ../../whatsnew/2.2.rst:323
msgid ""
"New-style classes also support a new method, "
"``__getattribute__(attr_name)``. The difference between the two methods is "
"that :meth:`~object.__getattribute__` is *always* called whenever any "
"attribute is accessed, while the old :meth:`~object.__getattr__` is only "
"called if ``foo`` isn't found in the instance's dictionary."
msgstr ""
#: ../../whatsnew/2.2.rst:329
msgid ""
"However, Python 2.2's support for :dfn:`properties` will often be a simpler "
"way to trap attribute references. Writing a :meth:`!__getattr__` method is "
"complicated because to avoid recursion you can't use regular attribute "
"accesses inside them, and instead have to mess around with the contents of :"
"attr:`~object.__dict__`. :meth:`~object.__getattr__` methods also end up "
"being called by Python when it checks for other methods such as :meth:"
"`~object.__repr__` or :meth:`!__coerce__`, and so have to be written with "
"this in mind. Finally, calling a function on every attribute access results "
"in a sizable performance loss."
msgstr ""
#: ../../whatsnew/2.2.rst:338
msgid ""
":class:`property` is a new built-in type that packages up three functions "
"that get, set, or delete an attribute, and a docstring. For example, if you "
"want to define a :attr:`!size` attribute that's computed, but also settable, "
"you could write::"
msgstr ""
#: ../../whatsnew/2.2.rst:343
msgid ""
"class C(object):\n"
" def get_size (self):\n"
" result = ... computation ...\n"
" return result\n"
" def set_size (self, size):\n"
" ... compute something based on the size\n"
" and set internal state appropriately ...\n"
"\n"
" # Define a property. The 'delete this attribute'\n"
" # method is defined as None, so the attribute\n"
" # can't be deleted.\n"
" size = property(get_size, set_size,\n"
" None,\n"
" \"Storage size of this instance\")"
msgstr ""
#: ../../whatsnew/2.2.rst:358
msgid ""
"That is certainly clearer and easier to write than a pair of :meth:`!"
"__getattr__`/:meth:`!__setattr__` methods that check for the :attr:`!size` "
"attribute and handle it specially while retrieving all other attributes from "
"the instance's :attr:`~object.__dict__`. Accesses to :attr:`!size` are also "
"the only ones which have to perform the work of calling a function, so "
"references to other attributes run at their usual speed."
msgstr ""
#: ../../whatsnew/2.2.rst:365
msgid ""
"Finally, it's possible to constrain the list of attributes that can be "
"referenced on an object using the new :attr:`~object.__slots__` class "
"attribute. Python objects are usually very dynamic; at any time it's "
"possible to define a new attribute on an instance by just doing ``obj."
"new_attr=1``. A new-style class can define a class attribute named :attr:"
"`~object.__slots__` to limit the legal attributes to a particular set of "
"names. An example will make this clear::"
msgstr ""
#: ../../whatsnew/2.2.rst:372
msgid ""
">>> class C(object):\n"
"... __slots__ = ('template', 'name')\n"
"...\n"
">>> obj = C()\n"
">>> print obj.template\n"
"None\n"
">>> obj.template = 'Test'\n"
">>> print obj.template\n"
"Test\n"
">>> obj.newattr = None\n"
"Traceback (most recent call last):\n"
" File \"<stdin>\", line 1, in ?\n"
"AttributeError: 'C' object has no attribute 'newattr'"
msgstr ""
#: ../../whatsnew/2.2.rst:386
msgid ""
"Note how you get an :exc:`AttributeError` on the attempt to assign to an "
"attribute not listed in :attr:`~object.__slots__`."
msgstr ""
#: ../../whatsnew/2.2.rst:393
msgid "Related Links"
msgstr "相關連結"
#: ../../whatsnew/2.2.rst:395
msgid ""
"This section has just been a quick overview of the new features, giving "
"enough of an explanation to start you programming, but many details have "
"been simplified or ignored. Where should you go to get a more complete "
"picture?"
msgstr ""
#: ../../whatsnew/2.2.rst:399
msgid ""
"The :ref:`descriptorhowto` is a lengthy tutorial introduction to the "
"descriptor features, written by Guido van Rossum. If my description has "
"whetted your appetite, go read this tutorial next, because it goes into much "
"more detail about the new features while still remaining quite easy to read."
msgstr ""
#: ../../whatsnew/2.2.rst:404
msgid ""
"Next, there are two relevant PEPs, :pep:`252` and :pep:`253`. :pep:`252` is "
"titled \"Making Types Look More Like Classes\", and covers the descriptor "
"API. :pep:`253` is titled \"Subtyping Built-in Types\", and describes the "
"changes to type objects that make it possible to subtype built-in objects. :"
"pep:`253` is the more complicated PEP of the two, and at a few points the "
"necessary explanations of types and meta-types may cause your head to "
"explode. Both PEPs were written and implemented by Guido van Rossum, with "
"substantial assistance from the rest of the Zope Corp. team."
msgstr ""
#: ../../whatsnew/2.2.rst:413
msgid ""
"Finally, there's the ultimate authority: the source code. Most of the "
"machinery for the type handling is in :file:`Objects/typeobject.c`, but you "
"should only resort to it after all other avenues have been exhausted, "
"including posting a question to python-list or python-dev."
msgstr ""
#: ../../whatsnew/2.2.rst:422
msgid "PEP 234: Iterators"
msgstr "PEP 234:疊代器"
#: ../../whatsnew/2.2.rst:424
msgid ""
"Another significant addition to 2.2 is an iteration interface at both the C "
"and Python levels. Objects can define how they can be looped over by "
"callers."
msgstr ""
#: ../../whatsnew/2.2.rst:427
msgid ""
"In Python versions up to 2.1, the usual way to make ``for item in obj`` work "
"is to define a :meth:`~object.__getitem__` method that looks something like "
"this::"
msgstr ""
#: ../../whatsnew/2.2.rst:430
msgid ""
"def __getitem__(self, index):\n"
" return <next item>"
msgstr ""
"def __getitem__(self, index):\n"
" return <next item>"
#: ../../whatsnew/2.2.rst:433
msgid ""
":meth:`~object.__getitem__` is more properly used to define an indexing "
"operation on an object so that you can write ``obj[5]`` to retrieve the "
"sixth element. It's a bit misleading when you're using this only to "
"support :keyword:`for` loops. Consider some file-like object that wants to "
"be looped over; the *index* parameter is essentially meaningless, as the "
"class probably assumes that a series of :meth:`~object.__getitem__` calls "
"will be made with *index* incrementing by one each time. In other words, "
"the presence of the :meth:`~object.__getitem__` method doesn't mean that "
"using ``file[5]`` to randomly access the sixth element will work, though it "
"really should."
msgstr ""
#: ../../whatsnew/2.2.rst:443
msgid ""
"In Python 2.2, iteration can be implemented separately, and :meth:`~object."
"__getitem__` methods can be limited to classes that really do support random "
"access. The basic idea of iterators is simple. A new built-in function, "
"``iter(obj)`` or ``iter(C, sentinel)``, is used to get an iterator. "
"``iter(obj)`` returns an iterator for the object *obj*, while ``iter(C, "
"sentinel)`` returns an iterator that will invoke the callable object *C* "
"until it returns *sentinel* to signal that the iterator is done."
msgstr ""
#: ../../whatsnew/2.2.rst:451
msgid ""
"Python classes can define an :meth:`!__iter__` method, which should create "
"and return a new iterator for the object; if the object is its own iterator, "
"this method can just return ``self``. In particular, iterators will usually "
"be their own iterators. Extension types implemented in C can implement a :c:"
"member:`~PyTypeObject.tp_iter` function in order to return an iterator, and "
"extension types that want to behave as iterators can define a :c:member:"
"`~PyTypeObject.tp_iternext` function."
msgstr ""
#: ../../whatsnew/2.2.rst:458
msgid ""
"So, after all this, what do iterators actually do? They have one required "
"method, :meth:`next`, which takes no arguments and returns the next value. "
"When there are no more values to be returned, calling :meth:`next` should "
"raise the :exc:`StopIteration` exception. ::"
msgstr ""
#: ../../whatsnew/2.2.rst:463
msgid ""
">>> L = [1,2,3]\n"
">>> i = iter(L)\n"
">>> print i\n"
"<iterator object at 0x8116870>\n"
">>> i.next()\n"
"1\n"
">>> i.next()\n"
"2\n"
">>> i.next()\n"
"3\n"
">>> i.next()\n"
"Traceback (most recent call last):\n"
" File \"<stdin>\", line 1, in ?\n"
"StopIteration\n"
">>>"
msgstr ""
">>> L = [1,2,3]\n"
">>> i = iter(L)\n"
">>> print i\n"
"<iterator object at 0x8116870>\n"
">>> i.next()\n"
"1\n"
">>> i.next()\n"
"2\n"
">>> i.next()\n"
"3\n"
">>> i.next()\n"
"Traceback (most recent call last):\n"
" File \"<stdin>\", line 1, in ?\n"
"StopIteration\n"
">>>"
#: ../../whatsnew/2.2.rst:479
msgid ""
"In 2.2, Python's :keyword:`for` statement no longer expects a sequence; it "
"expects something for which :func:`iter` will return an iterator. For "
"backward compatibility and convenience, an iterator is automatically "
"constructed for sequences that don't implement :meth:`!__iter__` or a :c:"
"member:`~PyTypeObject.tp_iter` slot, so ``for i in [1,2,3]`` will still "
"work. Wherever the Python interpreter loops over a sequence, it's been "
"changed to use the iterator protocol. This means you can do things like "
"this::"
msgstr ""
#: ../../whatsnew/2.2.rst:487
msgid ""
">>> L = [1,2,3]\n"
">>> i = iter(L)\n"
">>> a,b,c = i\n"
">>> a,b,c\n"
"(1, 2, 3)"
msgstr ""
">>> L = [1,2,3]\n"
">>> i = iter(L)\n"
">>> a,b,c = i\n"
">>> a,b,c\n"
"(1, 2, 3)"
#: ../../whatsnew/2.2.rst:493
msgid ""
"Iterator support has been added to some of Python's basic types. Calling :"
"func:`iter` on a dictionary will return an iterator which loops over its "
"keys::"
msgstr ""
#: ../../whatsnew/2.2.rst:496
msgid ""
">>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,\n"
"... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}\n"
">>> for key in m: print key, m[key]\n"
"...\n"
"Mar 3\n"
"Feb 2\n"
"Aug 8\n"
"Sep 9\n"
"May 5\n"
"Jun 6\n"
"Jul 7\n"
"Jan 1\n"
"Apr 4\n"
"Nov 11\n"
"Dec 12\n"
"Oct 10"
msgstr ""
">>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,\n"
"... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}\n"
">>> for key in m: print key, m[key]\n"
"...\n"
"Mar 3\n"
"Feb 2\n"
"Aug 8\n"
"Sep 9\n"
"May 5\n"
"Jun 6\n"
"Jul 7\n"
"Jan 1\n"
"Apr 4\n"
"Nov 11\n"
"Dec 12\n"
"Oct 10"
#: ../../whatsnew/2.2.rst:513
msgid ""
"That's just the default behaviour. If you want to iterate over keys, "
"values, or key/value pairs, you can explicitly call the :meth:`!iterkeys`, :"
"meth:`!itervalues`, or :meth:`!iteritems` methods to get an appropriate "
"iterator. In a minor related change, the :keyword:`in` operator now works on "
"dictionaries, so ``key in dict`` is now equivalent to ``dict.has_key(key)``."
msgstr ""
#: ../../whatsnew/2.2.rst:519
msgid ""
"Files also provide an iterator, which calls the :meth:`readline` method "
"until there are no more lines in the file. This means you can now read each "
"line of a file using code like this::"
msgstr ""
#: ../../whatsnew/2.2.rst:523
msgid ""
"for line in file:\n"
" # do something for each line\n"
" ..."
msgstr ""
#: ../../whatsnew/2.2.rst:527
msgid ""
"Note that you can only go forward in an iterator; there's no way to get the "
"previous element, reset the iterator, or make a copy of it. An iterator "
"object could provide such additional capabilities, but the iterator protocol "
"only requires a :meth:`next` method."
msgstr ""
#: ../../whatsnew/2.2.rst:535
msgid ":pep:`234` - Iterators"
msgstr ":pep:`234` - 疊代器"
#: ../../whatsnew/2.2.rst:536
msgid ""
"Written by Ka-Ping Yee and GvR; implemented by the Python Labs crew, mostly "
"by GvR and Tim Peters."
msgstr ""
"由 Ka-Ping Yee 和 GvR 撰寫;由 Python Labs 團隊實作,主要為 GvR 和 Tim "
"Peters 所貢獻。"
#: ../../whatsnew/2.2.rst:543
msgid "PEP 255: Simple Generators"
msgstr "PEP 255:簡單產生器"
#: ../../whatsnew/2.2.rst:545
msgid ""
"Generators are another new feature, one that interacts with the introduction "
"of iterators."
msgstr ""
#: ../../whatsnew/2.2.rst:548
msgid ""
"You're doubtless familiar with how function calls work in Python or C. When "
"you call a function, it gets a private namespace where its local variables "
"are created. When the function reaches a :keyword:`return` statement, the "
"local variables are destroyed and the resulting value is returned to the "
"caller. A later call to the same function will get a fresh new set of local "
"variables. But, what if the local variables weren't thrown away on exiting a "
"function? What if you could later resume the function where it left off? "
"This is what generators provide; they can be thought of as resumable "
"functions."
msgstr ""
#: ../../whatsnew/2.2.rst:557
msgid "Here's the simplest example of a generator function::"
msgstr ""
#: ../../whatsnew/2.2.rst:559
msgid ""
"def generate_ints(N):\n"
" for i in range(N):\n"
" yield i"
msgstr ""
"def generate_ints(N):\n"
" for i in range(N):\n"
" yield i"
#: ../../whatsnew/2.2.rst:563
msgid ""
"A new keyword, :keyword:`yield`, was introduced for generators. Any "
"function containing a :keyword:`!yield` statement is a generator function; "
"this is detected by Python's bytecode compiler which compiles the function "
"specially as a result. Because a new keyword was introduced, generators "
"must be explicitly enabled in a module by including a ``from __future__ "
"import generators`` statement near the top of the module's source code. In "
"Python 2.3 this statement will become unnecessary."
msgstr ""
#: ../../whatsnew/2.2.rst:571
msgid ""
"When you call a generator function, it doesn't return a single value; "
"instead it returns a generator object that supports the iterator protocol. "
"On executing the :keyword:`yield` statement, the generator outputs the value "
"of ``i``, similar to a :keyword:`return` statement. The big difference "
"between :keyword:`!yield` and a :keyword:`!return` statement is that on "
"reaching a :keyword:`!yield` the generator's state of execution is suspended "
"and local variables are preserved. On the next call to the generator's "
"``next()`` method, the function will resume executing immediately after the :"
"keyword:`!yield` statement. (For complicated reasons, the :keyword:`!yield` "
"statement isn't allowed inside the :keyword:`!try` block of a :keyword:"
"`try`...\\ :keyword:`finally` statement; read :pep:`255` for a full "
"explanation of the interaction between :keyword:`!yield` and exceptions.)"
msgstr ""
#: ../../whatsnew/2.2.rst:584
msgid "Here's a sample usage of the :func:`!generate_ints` generator::"
msgstr ""
#: ../../whatsnew/2.2.rst:586
msgid ""
">>> gen = generate_ints(3)\n"
">>> gen\n"
"<generator object at 0x8117f90>\n"
">>> gen.next()\n"
"0\n"
">>> gen.next()\n"
"1\n"
">>> gen.next()\n"
"2\n"
">>> gen.next()\n"
"Traceback (most recent call last):\n"
" File \"<stdin>\", line 1, in ?\n"
" File \"<stdin>\", line 2, in generate_ints\n"
"StopIteration"
msgstr ""
">>> gen = generate_ints(3)\n"
">>> gen\n"
"<generator object at 0x8117f90>\n"
">>> gen.next()\n"
"0\n"
">>> gen.next()\n"
"1\n"
">>> gen.next()\n"
"2\n"
">>> gen.next()\n"
"Traceback (most recent call last):\n"
" File \"<stdin>\", line 1, in ?\n"
" File \"<stdin>\", line 2, in generate_ints\n"
"StopIteration"
#: ../../whatsnew/2.2.rst:601
msgid ""
"You could equally write ``for i in generate_ints(5)``, or ``a,b,c = "
"generate_ints(3)``."
msgstr ""
#: ../../whatsnew/2.2.rst:604
msgid ""
"Inside a generator function, the :keyword:`return` statement can only be "
"used without a value, and signals the end of the procession of values; "
"afterwards the generator cannot return any further values. :keyword:`!"
"return` with a value, such as ``return 5``, is a syntax error inside a "
"generator function. The end of the generator's results can also be "
"indicated by raising :exc:`StopIteration` manually, or by just letting the "
"flow of execution fall off the bottom of the function."
msgstr ""
#: ../../whatsnew/2.2.rst:612
msgid ""
"You could achieve the effect of generators manually by writing your own "
"class and storing all the local variables of the generator as instance "
"variables. For example, returning a list of integers could be done by "
"setting ``self.count`` to 0, and having the :meth:`next` method increment "
"``self.count`` and return it. However, for a moderately complicated "
"generator, writing a corresponding class would be much messier. :file:`Lib/"
"test/test_generators.py` contains a number of more interesting examples. "
"The simplest one implements an in-order traversal of a tree using generators "
"recursively. ::"
msgstr ""
#: ../../whatsnew/2.2.rst:621
msgid ""