Here are all of the changes that Python 2.3 makes to the core Python language.
isinstance(int(expression), int)
is false, but that seems
unlikely to cause problems in practice.
list.insert(pos, value)
used to
insert value at the front of the list when pos was
negative. The behaviour has now been changed to be consistent with
slice indexing, so when pos is -1 the value will be inserted
before the last element, and so forth.
list.index(value)
, which searches for value
within the list and returns its index, now takes optional
start and stop arguments to limit the search to
only part of the list.
>>> d = {1:2} >>> d {1: 2} >>> d.pop(4) Traceback (most recent call last): File "stdin", line 1, in ? KeyError: 4 >>> d.pop(1) 2 >>> d.pop(1) Traceback (most recent call last): File "stdin", line 1, in ? KeyError: 'pop(): dictionary is empty' >>> d {} >>>
There's also a new class method,
dict.fromkeys(iterable, value), that
creates a dictionary with keys taken from the supplied iterator
iterable and all values set to value, defaulting to
None
.
(Patches contributed by Raymond Hettinger.)
Also, the dict() constructor now accepts keyword arguments to simplify creating small dictionaries:
>>> dict(red=1, blue=2, green=3, black=4) {'blue': 2, 'black': 4, 'green': 3, 'red': 1}
(Contributed by Just van Rossum.)
__debug__
flag, so you can no longer disable assertions by assigning to __debug__
.
Running Python with the -O switch will still generate
code that doesn't execute any assertions.
>>> import types >>> m = types.ModuleType('abc','docstring') >>> m <module 'abc' (built-in)> >>> m.__doc__ 'docstring'
raise "Error occurred"
, has begun. Raising a string will
now trigger PendingDeprecationWarning.
None
as a variable name will now result in a
SyntaxWarning warning. In a future version of Python,
None
may finally become a keyword.
for line in file_obj
. File objects also have a
new read-only encoding attribute that gives the encoding used
by the file; Unicode strings written to the file will be automatically
converted to bytes using the given encoding.
>>> s = socket.socket() >>> s.__class__ <type 'socket'>
In 2.3, you get this:
>>> s.__class__ <type '_socket.socket'>
X in Y
where X
and Y are strings, X could only be a single character.
That's now changed; X can be a string of any length, and
X in Y
will return True if X is a
substring of Y. If X is the empty string, the result is
always True.
>>> 'ab' in 'abcd' True >>> 'ad' in 'abcd' False >>> '' in 'abcd' True
Note that this doesn't tell you where the substring starts; if you need that information, use the find() string method.
>>> ' abc '.strip() 'abc' >>> '><><abc<><><>'.strip('<>') 'abc' >>> '><><abc<><><>\n'.strip('<>') 'abc<><><>\n' >>> u'\u4000\u4001abc\u4000'.strip(u'\u4000') u'\u4001abc' >>>
(Suggested by Simon Brunning and implemented by Walter Dörwald.)
%
operator is still more flexible and powerful
than zfill().
>>> '45'.zfill(4) '0045' >>> '12345'.zfill(4) '12345' >>> 'goofy'.zfill(6) '0goofy'
(Contributed by Walter Dörwald.)
isinstance(obj, basestring)
will return True for
either kind of string. It's a completely abstract type, so you
can't create basestring instances.
SET_LINENO
opcode is now gone. This may provide a
small speed increase, depending on your compiler's idiosyncrasies.
See section 20 for a longer explanation.
(Removed by Michael Hudson.)
for i in xrange(n)
slightly faster than
for i in range(n)
. (Patch by Raymond Hettinger.)
The net result of the 2.3 optimizations is that Python 2.3 runs the pystone benchmark around 25% faster than Python 2.2.
See About this document... for information on suggesting changes.