11\documentclass {howto }
2-
32% $Id$
43
54\title {What's New in Python 2.3}
1413% \section{Introduction \label{intro}}
1514
1615{\large This article is a draft, and is currently up to date for some
17- random version of the CVS tree around March 26 2002. Please send any
16+ random version of the CVS tree around May 26 2002. Please send any
1817additions, comments or errata to the author.}
1918
2019This article explains the new features in Python 2.3. The tentative
3433
3534% ======================================================================
3635\section {PEP 255: Simple Generators }
36+ \label {section-generators }
3737
3838In Python 2.2, generators were added as an optional feature, to be
3939enabled by a \code {from __future__ import generators} directive. In
@@ -184,12 +184,30 @@ \section{PEP 255: Simple Generators}
184184% ======================================================================
185185\section {PEP 278: Universal Newline Support }
186186
187- XXX write this section
188-
189- % Highlights: import and friends will understand any of \r, \n and \r\n
190- % as end of line. Python file input will do the same if you use mode 'U'.
191- % Everything can be disabled by configuring with --without-universal-newlines.
192-
187+ The three major operating systems used today are Microsoft Windows,
188+ Apple's Macintosh OS, and the various Unix derivatives. A minor
189+ irritation is that these three platforms all use different characters
190+ to mark the ends of lines in text files. Unix uses character 10, the
191+ ASCII linefeed, MacOS uses character 13, the ASCII carriage return,
192+ and Windows uses a two-character sequence of carriage return plus a
193+ newline.
194+
195+ Python's file objects can now support end of line conventions other
196+ than the one followed by the platform on which Python is running.
197+ Opening a file with the mode \samp {U} or \samp {rU} will open a file
198+ for reading in universal newline mode. All three line ending
199+ conventions will be translated to a \samp {\e n} in the strings
200+ returned by the various file methods such as \method {read()} and
201+ \method {readline()}.
202+
203+ Universal newline support is also used when importing modules and when
204+ executing a file with the \function {execfile()} function. This means
205+ that Python modules can be shared between all three operating systems
206+ without needing to convert the line-endings.
207+
208+ This feature can be disabled at compile-time by specifying the
209+ \longprogramopt {without-universal-newlines} when running Python's
210+ configure script.
193211
194212\begin {seealso }
195213
@@ -200,8 +218,72 @@ \section{PEP 278: Universal Newline Support}
200218
201219% ======================================================================
202220\section {PEP 285: The \class {bool} Type }
221+ \label {section-bool }
222+
223+ A Boolean type was added to Python 2.3. Two new constants were added
224+ to the \module {__builtin__} module, \constant {True} and
225+ \constant {False}. The type object for this new type is named
226+ \class {bool}; the constructor for it takes any Python value and
227+ converts it to \constant {True} or \constant {False}.
228+
229+ \begin {verbatim }
230+ >>> bool(1)
231+ True
232+ >>> bool(0)
233+ False
234+ >>> bool([])
235+ False
236+ >>> bool( (1,) )
237+ True
238+ \end {verbatim }
239+
240+ Most of the standard library modules and built-in functions have been
241+ changed to return Booleans.
242+
243+ \begin {verbatim }
244+ >>> o = []
245+ >>> hasattr(o, 'append')
246+ True
247+ >>> isinstance(o, list)
248+ True
249+ >>> isinstance(o, tuple)
250+ False
251+ \end {verbatim }
252+
253+ Python's Booleans were added with the primary goal of making code
254+ clearer. For example, if you're reading a function and encounter the
255+ statement \code {return 1}, you might wonder whether the \samp {1}
256+ represents a truth value, or whether it's an index, or whether it's a
257+ coefficient that multiplies some other quantity. If the statement is
258+ \code {return True}, however, the meaning of the return value is quite
259+ clearly a truth value.
260+
261+ Python's Booleans were not added for the sake of strict type-checking.
262+ A very strict language such as Pascal
263+ % XXX is Pascal the right example here?
264+ would also prevent you performing arithmetic with Booleans, and would
265+ require that the expression in an \keyword {if} statement always
266+ evaluate to a Boolean. Python is not this strict, and it never will
267+ be. (\pep {285} explicitly says this.) So you can still use any
268+ expression in an \keyword {if}, even ones that evaluate to a list or
269+ tuple or some random object, and the Boolean type is a subclass of the
270+ \class {int} class, so arithmetic using a Boolean still works.
271+
272+ \begin {verbatim }
273+ >>> True + 1
274+ 2
275+ >>> False + 1
276+ 1
277+ >>> False * 75
278+ 0
279+ >>> True * 75
280+ 75
281+ \end {verbatim }
203282
204- XXX write this section
283+ To sum up \constant {True} and \constant {False} in a sentence: they're
284+ alternative ways to spell the integer values 1 and 0, with the single
285+ difference that \function {str()} and \function {repr()} return the
286+ strings \samp {True} and \samp {False} instead of \samp {1} and \samp {0}.
205287
206288\begin {seealso }
207289
@@ -213,60 +295,158 @@ \section{PEP 285: The \class{bool} Type}
213295% ======================================================================
214296\section {New and Improved Modules }
215297
216- arraymodule.c: - add Py_UNICODE arrays
217- - support +=, *=
298+ As usual, Python's standard modules had a number of enhancements and
299+ bug fixes. Here's a partial list; consult the \file {Misc/NEWS} file
300+ in the source tree, or the CVS logs, for a more complete list.
301+
302+ \begin {itemize }
303+
304+ \item One minor but far-reaching change is that the names of extension
305+ types defined by the modules included with Python now contain the
306+ module and a \samp {.} in front of the type name. For example, in
307+ Python 2.2, if you created a socket and printed its
308+ \member {__class__}, you'd get this output:
309+
310+ \begin {verbatim }
311+ >>> s = socket.socket()
312+ >>> s.__class__
313+ <type 'socket'>
314+ \end {verbatim }
315+
316+ In 2.3, you get this:
317+ \begin {verbatim }
318+ >>> s.__class__
319+ <type '_socket.socket'>
320+ \end {verbatim }
321+
322+ \item The \method {strip()}, \method {lstrip()}, and \method {rstrip()}
323+ string methods now have an optional argument for specifying the
324+ characters to strip. The default is still to remove all whitespace
325+ characters:
326+
327+ \begin {verbatim }
328+ >>> ' abc '.strip()
329+ 'abc'
330+ >>> '><><abc<><><>'.strip('<>')
331+ 'abc'
332+ >>> '><><abc<><><>\n'.strip('<>')
333+ 'abc<><><>\n'
334+ >>> u'\u4000\u4001abc\u4000'.strip(u'\u4000')
335+ u'\u4001abc'
336+ >>>
337+ \end {verbatim }
338+
339+ \item Another new string method is \method {zfill()}, originally a
340+ function in the \module {string} module. \method {zfill()} pads a
341+ numeric string with zeros on the left until it's the specified width.
342+ Note that the \code {\% } operator is still more flexible and powerful
343+ than \method {zfill()}.
344+
345+ \begin {verbatim }
346+ >>> '45'.zfill(4)
347+ '0045'
348+ >>> '12345'.zfill(4)
349+ '12345'
350+ >>> 'goofy'.zfill(4)
351+ '0goofy'
352+ \end {verbatim }
353+
354+ \item Dictionaries have a new method, method{pop(\var {key})}, that
355+ returns the value corresponding to \var {key} and removes that
356+ key/value pair from the dictionary. \method {pop()} will raise a
357+ \exception {KeyError} if the requsted key isn't present in the
358+ dictionary:
359+
360+ \begin {verbatim }
361+ >>> d = {1:2}
362+ >>> d
363+ {1: 2}
364+ >>> d.pop(4)
365+ Traceback (most recent call last):
366+ File ``<stdin>'', line 1, in ?
367+ KeyError: 4
368+ >>> d.pop(1)
369+ 2
370+ >>> d.pop(1)
371+ Traceback (most recent call last):
372+ File ``<stdin>'', line 1, in ?
373+ KeyError: pop(): dictionary is empty
374+ >>> d
375+ {}
376+ >>>
377+ \end {verbatim }
218378
219379distutils: command/bdist_packager, support for Solaris pkgtool
220380and HP-UX swinstall
221381
222- Return enhanced tuples in grpmodule
223382
224- posixmodule: killpg, mknod, fchdir,
383+ \item Two new functions, \function {killpg()} and \function {mknod()},
384+ were added to the \module {posix} module that underlies the \module {os}
385+ module.
225386
226- Expat is now included with the Python source
387+ \item (XXX write this) arraymodule.c: - add Py_UNICODE arrays
388+ - support +=, *=
227389
228- Readline: Add get_history_item, get_current_history_length, and
229- redisplay functions.
390+ \item The \module {grp} module now returns enhanced tuples:
230391
231- Add optional arg to string methods strip(), lstrip(), rstrip().
232- The optional arg specifies characters to delete.
392+ \begin {verbatim }
393+ >>> import grp
394+ >>> g = grp.getgrnam('amk')
395+ >>> g.gr_name, g.gr_gid
396+ ('amk', 500)
397+ \end {verbatim }
233398
234- New method: string.zfill()
399+ \item The \module {readline} module also gained a number of new
400+ functions: \function {get_history_item()},
401+ \function {get_current_history_length()}, and \function {redisplay()}.
402+
403+ \end {itemize }
235404
236- Add dict method pop().
237405
238406New enumerate() built-in.
239407
240408% ======================================================================
241409\section {Interpreter Changes and Fixes }
242410
243- file object can now be subtyped (did this not work before?)
411+ Here are the changes that Python 2.3 makes to the core language.
412+
413+ \begin {itemize }
414+ \item The \keyword {yield} statement is now always a keyword, as
415+ described in section~\ref {section-generators }.
416+
417+ \item Two new constants, \constant {True} and \constant {False} were
418+ added along with the built-in \class {bool} type, as described in
419+ section~\ref {section-bool }.
420+
421+ \item The \class {file} type can now be subtyped. (XXX did this not work
422+ before? Thought I used it in an example in the 2.2 What's New document...)
244423
245- yield is now always available
424+ \item File objects also manage their internal string buffer
425+ differently by increasing it exponentially when needed.
426+ This results in the benchmark tests in \file {Lib/test/test_bufio.py}
427+ speeding up from 57 seconds to 1.7 seconds, according to one
428+ measurement.
246429
247- This adds the module name and a dot in front of the type name in every
248- type object initializer, except for built-in types (and those that
249- already had this). Note that it touches lots of Mac modules -- I have
250- no way to test these but the changes look right. Apologies if they're
251- not. This also touches the weakref docs, which contains a sample type
252- object initializer. It also touches the mmap test output, because the
253- mmap type's repr is included in that output. It touches object.h to
254- put the correct description in a comment.
430+ \end {itemize }
255431
256- File objects: Grow the string buffer at a mildly exponential rate for
257- the getc version of get_line. This makes test_bufio finish in 1.7
258- seconds instead of 57 seconds on my machine (with Py_DEBUG defined).
259432
260433% ======================================================================
261434\section {Other Changes and Fixes }
262435
436+ XXX write this
437+
263438The tools used to build the documentation now work under Cygwin as
264439well as \UNIX .
265440
441+
266442% ======================================================================
267- \section {C Interface Changes }
443+ \section {Build and C API Changes }
444+
445+ XXX write this
268446
269- Patch \# 527027: Allow building python as shared library with
447+ \begin {itemize }
448+
449+ \item Patch \# 527027: Allow building python as shared library with
270450--enable-shared
271451
272452pymalloc is now enabled by default (also mention debug-mode pymalloc)
@@ -277,6 +457,10 @@ \section{C Interface Changes}
277457
278458PyArg_NoArgs macro is now deprecated
279459
460+ \item The source code for the Expat XML parser is now included with
461+ the Python source, so the \module {pyexpat} module is no longer
462+ dependent on having a system library containing Expat.
463+
280464===
281465Introduce two new flag bits that can be set in a PyMethodDef method
282466descriptor, as used for the tp_methods slot of a type. These new flag
@@ -297,7 +481,11 @@ \section{C Interface Changes}
297481ValueError will be raised if these flags are found in that context.
298482===
299483
300- Ports:
484+ \end {itemize }
485+
486+ \subsection {Port-Specific Changes }
487+
488+ XXX write this
301489
302490OS/2 EMX port
303491
@@ -319,6 +507,3 @@ \section{Acknowledgements \label{acks}}
319507article: Fred~L. Drake, Jr.
320508
321509\end {document }
322-
323-
324-
0 commit comments