Some smaller changes made to the core Python language are:
- The syntax for set literals has been backported from Python 3.x. Curly brackets are used to surround the contents of the resulting mutable set; set literals are distinguished from dictionaries by not containing colons and values.
{}
continues to represent an empty dictionary; useset()
for an empty set.Backported by Alexandre Vassalotti; bpo-2335. - Dictionary and set comprehensions are another feature backported from 3.x, generalizing list/generator comprehensions to use the literal syntax for sets and dictionaries.Backported by Alexandre Vassalotti; bpo-2333.
- The
with
statement can now use multiple context managers in one statement. Context managers are processed from left to right and each one is treated as beginning a newwith
statement. This means that:is equivalent to:Thecontextlib.nested()
function provides a very similar function, so it’s no longer necessary and has been deprecated.(Proposed in https://codereview.appspot.com/53094; implemented by Georg Brandl.) - Conversions between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places:
str()
on floats and complex numbers; thefloat
andcomplex
constructors; numeric formatting; serializing and deserializing floats and complex numbers using themarshal
,pickle
andjson
modules; parsing of float and imaginary literals in Python code; andDecimal
-to-float conversion.Related to this, therepr()
of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding (with round-half-to-even rounding mode). Previously it gave a string based on rounding x to 17 decimal digits.The rounding library responsible for this improvement works on Windows and on Unix platforms using the gcc, icc, or suncc compilers. There may be a small number of platforms where correct operation of this code cannot be guaranteed, so the code is not used on such systems. You can find out which code is being used by checkingsys.float_repr_style
, which will beshort
if the new code is in use andlegacy
if it isn’t.Implemented by Eric Smith and Mark Dickinson, using David Gay’sdtoa.c
library; bpo-7117. - Conversions from long integers and regular integers to floating point now round differently, returning the floating-point number closest to the number. This doesn’t matter for small integers that can be converted exactly, but for large numbers that will unavoidably lose precision, Python 2.7 now approximates more closely. For example, Python 2.6 computed the following:Python 2.7’s floating-point result is larger, but much closer to the true value:(Implemented by Mark Dickinson; bpo-3166.)Integer division is also more accurate in its rounding behaviours. (Also implemented by Mark Dickinson; bpo-1811.)
- Implicit coercion for complex numbers has been removed; the interpreter will no longer ever attempt to call a
__coerce__()
method on complex objects. (Removed by Meador Inge and Mark Dickinson; bpo-5211.) - The
str.format()
method now supports automatic numbering of the replacement fields. This makes usingstr.format()
more closely resemble using%s
formatting:The auto-numbering takes the fields from left to right, so the first{...}
specifier will use the first argument tostr.format()
, the next specifier will use the next argument, and so on. You can’t mix auto-numbering and explicit numbering – either number all of your specifier fields or none of them – but you can mix auto-numbering and named fields, as in the second example above. (Contributed by Eric Smith; bpo-5237.)Complex numbers now correctly support usage withformat()
, and default to being right-aligned. Specifying a precision or comma-separation applies to both the real and imaginary parts of the number, but a specified field width and alignment is applied to the whole of the resulting1.5+3j
output. (Contributed by Eric Smith; bpo-1588 and bpo-7988.)The ‘F’ format code now always formats its output using uppercase characters, so it will now produce ‘INF’ and ‘NAN’. (Contributed by Eric Smith; bpo-3382.)A low-level change: theobject.__format__()
method now triggers aPendingDeprecationWarning
if it’s passed a format string, because the__format__()
method forobject
converts the object to a string representation and formats that. Previously the method silently applied the format string to the string representation, but that could hide mistakes in Python code. If you’re supplying formatting information such as an alignment or precision, presumably you’re expecting the formatting to be applied in some object-specific way. (Fixed by Eric Smith; bpo-7994.) - The
int()
andlong()
types gained abit_length
method that returns the number of bits necessary to represent its argument in binary:(Contributed by Fredrik Johansson and Victor Stinner; bpo-3439.) - It’s now possible for a subclass of the built-in
unicode
type to override the__unicode__()
method. (Implemented by Victor Stinner; bpo-1583863.) - When using
@classmethod
and@staticmethod
to wrap methods as class or static methods, the wrapper object now exposes the wrapped function as their__func__
attribute. (Contributed by Amaury Forgeot d’Arc, after a suggestion by George Sakkis; bpo-5982.) - When a restricted set of attributes were set using
__slots__
, deleting an unset attribute would not raiseAttributeError
as you would expect. Fixed by Benjamin Peterson; bpo-7604.) - Two new encodings are now supported: “cp720”, used primarily for Arabic text; and “cp858”, a variant of CP 850 that adds the euro symbol. (CP720 contributed by Alexander Belchenko and Amaury Forgeot d’Arc in bpo-1616979; CP858 contributed by Tim Hatch in bpo-8016.)
- The
file
object will now set thefilename
attribute on theIOError
exception when trying to open a directory on POSIX platforms (noted by Jan Kaliszewski; bpo-4764), and now explicitly checks for and forbids writing to read-only file objects instead of trusting the C library to catch and report the error (fixed by Stefan Krah; bpo-5677). - The Python tokenizer now translates line endings itself, so the
compile()
built-in function now accepts code using any line-ending convention. Additionally, it no longer requires that the code end in a newline. - Extra parentheses in function definitions are illegal in Python 3.x, meaning that you get a syntax error from
def f((x)): pass
. In Python3-warning mode, Python 2.7 will now warn about this odd usage. (Noted by James Lingard; bpo-7362.) - It’s now possible to create weak references to old-style class objects. New-style classes were always weak-referenceable. (Fixed by Antoine Pitrou; bpo-8268.)
- When a module object is garbage-collected, the module’s dictionary is now only cleared if no one else is holding a reference to the dictionary (bpo-7140)
No comments:
Post a Comment