Що нового в Python 2.4¶
- Автор:
A.M. Kuchling
У цій статті пояснюються нові функції Python 2.4.1, випущеного 30 березня 2005 року.
Python 2.4 — випуск середнього розміру. Він не вносить стільки змін, як радикальний Python 2.2, але пропонує більше функцій, ніж консервативний випуск 2.3. Найважливішими новими можливостями мови є декоратори функцій і вирази-генератори; більшість інших змін стосуються стандартної бібліотеки.
Згідно з журналами змін CVS, було застосовано 481 патч і 502 виправлені помилки між Python 2.3 і 2.4. Обидві цифри, ймовірно, занижені.
Ця стаття не намагається надати повну специфікацію кожної окремої нової функції, а натомість містить короткий вступ до кожної функції. Щоб отримати повну інформацію, зверніться до документації для Python 2.4, наприклад довідника бібліотеки Python і довідкового посібника Python. Часто вас направлять до PEP щодо конкретної нової функції для пояснень реалізації та обґрунтування дизайну.
PEP 218: Вбудовані об’єкти¶
Python 2.3 introduced the sets module. C implementations of set data
types have now been added to the Python core as two new built-in types,
set(iterable) and frozenset(iterable). They provide high speed
operations for membership testing, for eliminating duplicates from sequences,
and for mathematical operations like unions, intersections, differences, and
symmetric differences.
>>> a = set('abracadabra') # form a set from a string
>>> 'z' in a # fast membership testing
False
>>> a # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> ''.join(a) # convert back into a string
'arbcd'
>>> b = set('alacazam') # form a second set
>>> a - b # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b # letters in both a and b
set(['a', 'c'])
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a.add('z') # add a new element
>>> a.update('wxy') # add multiple new elements
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
>>> a.remove('x') # take one element out
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
Тип frozenset() є незмінною версією set(). Оскільки він незмінний і хешується, його можна використовувати як ключ словника або як член іншого набору.
The sets module remains in the standard library, and may be useful if you
wish to subclass the Set or ImmutableSet classes. There are
currently no plans to deprecate the module.
Дивись також
- PEP 218 - Додавання вбудованого типу об’єкта Set
Спочатку запропонований Грегом Вілсоном і остаточно реалізований Реймондом Геттінгером.
PEP 237: Об’єднання довгих і цілих чисел¶
Тривалий процес переходу для цього PEP, розпочатий у Python 2.2, робить ще один крок вперед у Python 2.4. У версії 2.3 певні цілочисельні операції, які поводилися б інакше після об’єднання int/long, викликали попередження FutureWarning і повертали значення, обмежені 32 або 64 бітами (залежно від вашої платформи). У версії 2.4 ці вирази більше не створюють попередження, а натомість створюють інший результат, який зазвичай є довгим цілим числом.
Проблемними виразами є, перш за все, зрушення вліво та довгі шістнадцяткові та вісімкові константи. Наприклад, 2 << 32 призводить до попередження у 2.3, оцінюючи 0 на 32-розрядних платформах. У Python 2.4 цей вираз тепер повертає правильну відповідь, 8589934592.
Дивись також
- PEP 237 - Об’єднання довгих і цілих чисел
Оригінальний PEP, написаний Моше Цадка та GvR. Зміни у версії 2.4 вніс Калле Свенссон.
PEP 289: Генератор виразів¶
The iterator feature introduced in Python 2.2 and the itertools module
make it easier to write programs that loop through large data sets without
having the entire data set in memory at one time. List comprehensions don’t fit
into this picture very well because they produce a Python list object containing
all of the items. This unavoidably pulls all of the objects into memory, which
can be a problem if your data set is very large. When trying to write a
functionally styled program, it would be natural to write something like:
links = [link for link in get_all_links() if not link.followed]
for link in links:
...
замість
for link in get_all_links():
if link.followed:
continue
...
Перша форма більш лаконічна і, можливо, більш читабельна, але якщо ви маєте справу з великою кількістю об’єктів посилань, вам доведеться написати другу форму, щоб уникнути одночасного зберігання всіх об’єктів посилань у пам’яті.
Вирази генератора працюють так само, як і розуміння списків, але не матеріалізують весь список; натомість вони створюють генератор, який повертатиме елементи один за одним. Наведений вище приклад можна записати так:
links = (link for link in get_all_links() if not link.followed)
for link in links:
...
Вирази-генератори завжди мають бути записані в круглих дужках, як у прикладі вище. Дужки, що сигналізують про виклик функції, також враховуються, тому, якщо ви хочете створити ітератор, який буде негайно передано функції, ви можете написати:
print sum(obj.count for obj in list_all_objects())
Вирази генератора відрізняються від розуміння списків кількома дрібницями. Зокрема, змінна циклу (obj у наведеному вище прикладі) недоступна поза виразом генератора. Розуміння списків залишає змінну присвоєною її останньому значенню; майбутні версії Python змінять це, зробивши розуміння списку відповідним виразам генератора в цьому відношенні.
Дивись також
- PEP 289 - Генератор виразів
Запропоновано Raymond Hettinger і реалізовано Jiwon Seo з ранніми зусиллями під керівництвом Hye-Shik Chang.
PEP 292: Простіші заміни рядків¶
Деякі нові класи в стандартній бібліотеці забезпечують альтернативний механізм для підстановки змінних у рядки; цей стиль заміни може бути кращим для додатків, де ненавченим користувачам потрібно редагувати шаблони.
Звичайним способом заміни змінних за іменем є оператор %:
>>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
'2: The Best of Times'
Під час написання рядка шаблону може бути легко забути i або s після закриваючої дужки. Це не є великою проблемою, якщо шаблон міститься в модулі Python, тому що ви запускаєте код, отримуєте «Символ непідтримуваного формату» ValueError і вирішуєте проблему. Однак розгляньте такий додаток, як Mailman, у якому рядки шаблону або переклади редагуються користувачами, які не знають мови Python. Синтаксис форматного рядка складно пояснити таким користувачам, і якщо вони роблять помилку, їм важко надати корисний відгук.
PEP 292 adds a Template class to the string module that uses
$ to indicate a substitution:
>>> import string
>>> t = string.Template('$page: $title')
>>> t.substitute({'page':2, 'title': 'The Best of Times'})
'2: The Best of Times'
If a key is missing from the dictionary, the substitute()
method will raise a KeyError. There’s also a
safe_substitute() method that ignores missing keys:
>>> t = string.Template('$page: $title')
>>> t.safe_substitute({'page':3})
'3: $title'
Дивись також
- PEP 292 - Простіші підстановки рядків
Написав і реалізував Баррі Варшау.
PEP 318: Декоратори для функцій і методів¶
Python 2.2 розширив об’єктну модель Python, додавши статичні методи та методи класу, але не розширив синтаксис Python, щоб надати новий спосіб визначення статичних методів або методів класу. Замість цього вам потрібно було написати оператор def у звичайний спосіб і передати отриманий метод у функцію staticmethod() або classmethod(), яка завершувала б функцію як метод новий тип. Ваш код виглядатиме так:
class C:
def meth (cls):
...
meth = classmethod(meth) # Rebind name to wrapped-up class method
Якби метод був дуже довгим, було б легко пропустити або забути виклик classmethod() після тіла функції.
Намір завжди полягав у тому, щоб додати певний синтаксис, щоб зробити такі визначення більш читабельними, але на момент випуску версії 2.2 хороший синтаксис не був очевидним. Сьогодні хороший синтаксис все ще неочевидний, але користувачі просять полегшити доступ до функції; для задоволення цієї потреби додано нову синтаксичну функцію.
Нова функція називається «декоратори функцій». Назва походить від ідеї, що classmethod(), staticmethod() та друзі зберігають додаткову інформацію про об’єкт функції; вони прикрашають функції більшою кількістю деталей.
Позначення запозичено з Java і використовує символ '@' як індикатор. Використовуючи новий синтаксис, приклад вище буде написаний:
class C:
@classmethod
def meth (cls):
...
@classmethod є скороченням для meth=classmethod(meth) призначення. Загалом, якщо у вас є:
@A
@B
@C
def f ():
...
Це еквівалентно такому попередньому коду декоратора:
def f(): ...
f = A(B(C(f)))
Декоратори мають бути в рядку перед визначенням функції, по одному декоратору на рядок, і не можуть бути в одному рядку з інструкцією def, тобто @A def f(): ... є незаконним. Ви можете лише прикрашати визначення функцій на рівні модуля або всередині класу; ви не можете прикрасити визначення класу.
Декоратор — це лише функція, яка приймає декоровану функцію як аргумент і повертає або ту саму функцію, або новий об’єкт. Значення, що повертається декоратором, не повинно бути викликаним (хоча зазвичай це так), якщо до результату не будуть застосовані інші декоратори. Легко написати власні декоратори. Наступний простий приклад просто встановлює атрибут для об’єкта функції:
>>> def deco(func):
... func.attr = 'decorated'
... return func
...
>>> @deco
... def f(): pass
...
>>> f
<function f at 0x402ef0d4>
>>> f.attr
'decorated'
>>>
Як трохи більш реалістичний приклад, наступний декоратор перевіряє, що наданий аргумент є цілим числом:
def require_int (func):
def wrapper (arg):
assert isinstance(arg, int)
return func(arg)
return wrapper
@require_int
def p1 (arg):
print arg
@require_int
def p2(arg):
print arg*2
Приклад у PEP 318 містить моднішу версію цієї ідеї, яка дозволяє як вказати необхідний тип, так і перевірити повернутий тип.
Функції-декоратори можуть приймати аргументи. Якщо надано аргументи, ваша функція-декоратор викликається лише з цими аргументами та має повернути нову функцію-декоратор; ця функція повинна приймати одну функцію та повертати функцію, як описано раніше. Іншими словами, @A @B @C(args) стає:
def f(): ...
_deco = C(args)
f = A(B(_deco(f)))
Зробити це правильно може трохи заморочити мозок, але це не надто складно.
A small related change makes the func_name
attribute of functions
writable. This attribute is used to display function names in tracebacks, so
decorators should change the name of any new function that’s constructed and
returned.
Дивись також
- PEP 318 - Декоратори для функцій, методів і класів
Автори: Кевін Д. Сміт, Джим Джеветт і Скіп Монтанаро. Кілька людей написали патчі, що реалізують декоратори функцій, але фактично зареєстровано патч №979728, написаний Марком Расселом.
- https://wiki.python.org/moin/PythonDecoratorLibrary
Ця сторінка Wiki містить кілька прикладів декораторів.
PEP 322: Зворотна ітерація¶
Нова вбудована функція, reversed(seq), приймає послідовність і повертає ітератор, який перебирає елементи послідовності у зворотному порядку.
>>> for i in reversed(xrange(1,4)):
... print i
...
3
2
1
Порівняно з розширеним нарізанням, таким як range(1,4)[::-1], reversed() легше читати, працює швидше та використовує значно менше пам’яті.
Зауважте, що reversed() приймає лише послідовності, а не довільні ітератори. Якщо ви хочете змінити ітератор, спочатку перетворіть його на список за допомогою list().
>>> input = open('/etc/passwd', 'r')
>>> for line in reversed(list(input)):
... print line
...
root:*:0:0:System Administrator:/var/root:/bin/tcsh
...
Дивись також
- PEP 322 - Зворотна ітерація
Написаний і реалізований Раймондом Геттінгером.
PEP 324: Новий модуль підпроцесу¶
The standard library provides a number of ways to execute a subprocess, offering
different features and different levels of complexity.
os.system(command) is easy to use, but slow (it runs a shell process
which executes the command) and dangerous (you have to be careful about escaping
the shell’s metacharacters). The popen2 module offers classes that can
capture standard output and standard error from the subprocess, but the naming
is confusing. The subprocess module cleans this up, providing a unified
interface that offers all the features you might need.
Instead of popen2’s collection of classes, subprocess contains a
single class called subprocess.Popen whose constructor supports a number of
different keyword arguments.
class Popen(args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0):
args зазвичай є послідовністю рядків, які будуть аргументами програми, що виконується як підпроцес. (Якщо аргумент shell має значення true, args може бути рядком, який потім буде передано в оболонку для інтерпретації, як це робить os.system().)
stdin, stdout і stderr визначають, якими будуть вхідні, вихідні та помилкові потоки підпроцесу. Ви можете надати файловий об’єкт або дескриптор файлу, або ви можете використовувати константу subprocess.PIPE для створення каналу між підпроцесом і батьківським процесом.
Конструктор має ряд зручних опцій:
close_fds просить закрити всі файлові дескриптори перед запуском підпроцесу.
cwd визначає робочий каталог, у якому виконуватиметься підпроцес (за замовчуванням це будь-який робочий каталог батьківського процесу).
env — це словник, який визначає змінні середовища.
preexec_fn — це функція, яка викликається перед запуском дочірнього елемента.
universal_newlines відкриває введення та виведення дочірнього елемента за допомогою функції Python universal newlines.
Once you’ve created the Popen instance, you can call its wait()
method to pause until the subprocess has exited, poll() to check if it’s
exited without pausing, or communicate(data) to send the string data
to the subprocess’s standard input. communicate(data) then reads any
data that the subprocess has sent to its standard output or standard error,
returning a tuple (stdout_data, stderr_data).
call() is a shortcut that passes its arguments along to the Popen
constructor, waits for the command to complete, and returns the status code of
the subprocess. It can serve as a safer analog to os.system():
sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
if sts == 0:
# Success
...
else:
# dpkg returned an error
...
Команда викликається без використання оболонки. Якщо ви дійсно хочете використовувати оболонку, ви можете додати shell=True як ключовий аргумент і надати рядок замість послідовності:
sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)
PEP бере різні приклади оболонки та коду Python і показує, як вони можуть бути переведені в код Python, який використовує subprocess. Настійно рекомендується прочитати цей розділ PEP.
Дивись також
- PEP 324 - підпроцес - Новий модуль процесу
Написано та реалізовано Пітером Естрандом за сприяння Фредріка Лунда та інших.
PEP 327: десятковий тип даних¶
Python has always supported floating-point (FP) numbers, based on the underlying
C double type, as a data type. However, while most programming
languages provide a floating-point type, many people (even programmers) are
unaware that floating-point numbers don’t represent certain decimal fractions
accurately. The new Decimal type can represent these fractions
accurately, up to a user-specified precision limit.
Навіщо потрібен Decimal?¶
Обмеження виникають через представлення чисел з плаваючою комою. Номери FP складаються з трьох компонентів:
Знак, який є позитивним чи негативним.
Мантиса, яка є одноцифровим двійковим числом, після якого йде дробова частина. Наприклад, «1,01» у нотації з основою 2 — це «1 + 0/2 + 1/4» або 1,25 у десятковій системі числення.
Експонента, яка вказує, де в представленому числі розташована кома.
Наприклад, число 1,25 має додатний знак, значення мантиси 1,01 (у двійковій системі) і експоненту 0 (десяткову кому не потрібно зміщувати). Число 5 має той самий знак і мантису, але показник степеня дорівнює 2, оскільки мантиса множиться на 4 (2 у ступені степеня 2); 1,25 * 4 дорівнює 5.
Modern systems usually provide floating-point support that conforms to a
standard called IEEE 754. C’s double type is usually implemented as a
64-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This
means that numbers can only be specified to 52 bits of precision. If you’re
trying to represent numbers whose expansion repeats endlessly, the expansion is
cut off after 52 bits. Unfortunately, most software needs to produce output in
base 10, and common fractions in base 10 are often repeating decimals in binary.
For example, 1.1 decimal is binary 1.0001100110011 ...; .1 = 1/16 + 1/32 +
1/256 plus an infinite number of additional terms. IEEE 754 has to chop off
that infinitely repeated decimal after 52 digits, so the representation is
slightly inaccurate.
Іноді ви можете побачити цю неточність, коли надруковано номер:
>>> 1.1
1.1000000000000001
Неточність не завжди помітна, коли ви друкуєте число, тому що перетворення FP-рядка в десятковий рядок забезпечується бібліотекою C, і більшість бібліотек C намагаються створити розумний результат. Однак навіть якщо вона не відображається, неточність все одно існує, і наступні операції можуть збільшити помилку.
Для багатьох програм це не має значення. Якщо я малюю точки та відображаю їх на моніторі, різниця між 1,1 і 1,1000000000000001 надто мала, щоб її було видно. Звіти часто обмежують виведення певною кількістю знаків після коми, і якщо округлити число до двох, трьох або навіть восьми знаків після коми, помилка ніколи не буде очевидною. Однак для додатків, де це має значення, реалізація власних користувацьких арифметичних процедур вимагає багато роботи.
Hence, the Decimal type was created.
The Decimal type¶
A new module, decimal, was added to Python’s standard library. It
contains two classes, Decimal and Context. Decimal
instances represent numbers, and Context instances are used to wrap up
various settings such as the precision and default rounding mode.
Decimal instances are immutable, like regular Python integers and FP
numbers; once it’s been created, you can’t change the value an instance
represents. Decimal instances can be created from integers or
strings:
>>> import decimal
>>> decimal.Decimal(1972)
Decimal("1972")
>>> decimal.Decimal("1.1")
Decimal("1.1")
Ви також можете надати кортежі, що містять знак, мантису, представлену як кортеж десяткових цифр, і експоненту:
>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
Decimal("-14.75")
Застереження: знаковий біт є логічним значенням, тому 0 є позитивним, а 1 – негативним.
Converting from floating-point numbers poses a bit of a problem: should the FP
number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
plus whatever inaccuracies are introduced? The decision was to dodge the issue
and leave such a conversion out of the API. Instead, you should convert the
floating-point number into a string using the desired precision and pass the
string to the Decimal constructor:
>>> f = 1.1
>>> decimal.Decimal(str(f))
Decimal("1.1")
>>> decimal.Decimal('%.12f' % f)
Decimal("1.100000000000")
Once you have Decimal instances, you can perform the usual mathematical
operations on them. One limitation: exponentiation requires an integer
exponent:
>>> a = decimal.Decimal('35.72')
>>> b = decimal.Decimal('1.73')
>>> a+b
Decimal("37.45")
>>> a-b
Decimal("33.99")
>>> a*b
Decimal("61.7956")
>>> a/b
Decimal("20.64739884393063583815028902")
>>> a ** 2
Decimal("1275.9184")
>>> a**b
Traceback (most recent call last):
...
decimal.InvalidOperation: x ** (non-integer)
You can combine Decimal instances with integers, but not with
floating-point numbers:
>>> a + 4
Decimal("39.72")
>>> a + 4.5
Traceback (most recent call last):
...
TypeError: You can interact Decimal only with int, long or Decimal data types.
>>>
Decimal numbers can be used with the math and cmath
modules, but note that they’ll be immediately converted to floating-point
numbers before the operation is performed, resulting in a possible loss of
precision and accuracy. You’ll also get back a regular floating-point number
and not a Decimal.
>>> import math, cmath
>>> d = decimal.Decimal('123456789012.345')
>>> math.sqrt(d)
351364.18288201344
>>> cmath.sqrt(-d)
351364.18288201344j
Decimal instances have a sqrt() method that returns a
Decimal, but if you need other things such as trigonometric functions
you’ll have to implement them.
>>> d.sqrt()
Decimal("351364.1828820134592177245001")
The Context type¶
Instances of the Context class encapsulate several settings for
decimal operations:
precis the precision, the number of decimal places.roundingspecifies the rounding mode. Thedecimalmodule has constants for the various possibilities:ROUND_DOWN,ROUND_CEILING,ROUND_HALF_EVEN, and various others.trapsis a dictionary specifying what happens on encountering certain error conditions: either an exception is raised or a value is returned. Some examples of error conditions are division by zero, loss of precision, and overflow.
There’s a thread-local default context available by calling getcontext();
you can change the properties of this context to alter the default precision,
rounding, or trap handling. The following example shows the effect of changing
the precision of the default context:
>>> decimal.getcontext().prec
28
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1428571428571428571428571429")
>>> decimal.getcontext().prec = 9
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.142857143")
Дію за умовчанням для умов помилки можна вибрати; модуль може або повертати спеціальне значення, наприклад нескінченність або не-число, або можуть бути викликані винятки:
>>> decimal.Decimal(1) / decimal.Decimal(0)
Traceback (most recent call last):
...
decimal.DivisionByZero: x / 0
>>> decimal.getcontext().traps[decimal.DivisionByZero] = False
>>> decimal.Decimal(1) / decimal.Decimal(0)
Decimal("Infinity")
>>>
The Context instance also has various methods for formatting numbers
such as to_eng_string() and to_sci_string().
Для отримання додаткової інформації перегляньте документацію до модуля decimal, яка містить короткий посібник і довідку.
Дивись також
- PEP 327 - десятковий тип даних
Написаний Факундо Батістою та реалізований Факундо Батістою, Еріком Прайсом, Реймондом Геттінгером, Аазом і Тімом Пітерсом.
- http://www.lahey.com/float.htm
У статті використовується код Fortran, щоб проілюструвати багато проблем, до яких може призвести неточність числа з плаваючою комою.
- https://speleotrove.com/decimal/
Опис десяткового представлення. Це представлення пропонується як стандарт і лежить в основі нового десяткового типу Python. Значну частину цього матеріалу написав Майк Коулішоу, розробник мови Rexx.
PEP 328: Багаторядковий імпорт¶
Одна зміна мови — це невелике синтаксичне налаштування, спрямоване на полегшення імпорту багатьох імен із модуля. У операторі from module import names names — це послідовність імен, розділених комами. Якщо послідовність дуже довга, ви можете або написати кілька імпортів з одного модуля, або ви можете використовувати зворотні похилі риски, щоб уникнути закінчення рядків, як це:
from SimpleXMLRPCServer import SimpleXMLRPCServer,\
SimpleXMLRPCRequestHandler,\
CGIXMLRPCRequestHandler,\
resolve_dotted_attribute
Синтаксичні зміни в Python 2.4 просто дозволяють поміщати імена в дужки. Python ігнорує нові рядки у виразі в дужках, тому зворотна похила риска більше не потрібна:
from SimpleXMLRPCServer import (SimpleXMLRPCServer,
SimpleXMLRPCRequestHandler,
CGIXMLRPCRequestHandler,
resolve_dotted_attribute)
PEP також пропонує, щоб усі оператори import були абсолютними імпортами, із символом . на початку, який вказує на відносний імпорт. Ця частина PEP не була реалізована для Python 2.4, але була завершена для Python 2.5.
Дивись також
- PEP 328 - Імпорт: багаторядковий і абсолютний/відносний
Написав Ааз. Багаторядковий імпорт реалізував Діма Дорфман.
PEP 331: Незалежне від мови перетворення Float/String¶
The locale modules lets Python software select various conversions and
display conventions that are localized to a particular country or language.
However, the module was careful to not change the numeric locale because various
functions in Python’s implementation required that the numeric locale remain set
to the 'C' locale. Often this was because the code was using the C
library’s atof() function.
Однак відсутність числової локалі спричиняла проблеми для розширень, які використовували сторонні бібліотеки C, оскільки вони не мали правильного налаштування локалі. Мотивуючим прикладом був GTK+, віджети інтерфейсу користувача якого не відображали числа в поточній локалі.
Рішення, описане в PEP, полягає в додаванні трьох нових функцій до API Python, які виконують перетворення лише ASCII, ігноруючи налаштування мови:
PyOS_ascii_strtod(str, ptr)andPyOS_ascii_atof(str, ptr)both convert a string to a C double.PyOS_ascii_formatd(buffer, buf_len, format, d)converts a double to an ASCII string.
The code for these functions came from the GLib library
(https://developer-old.gnome.org/glib/2.26/), whose developers kindly
relicensed the relevant functions and donated them to the Python Software
Foundation. The locale module can now change the numeric locale,
letting extensions such as GTK+ produce the correct results.
Дивись також
- PEP 331 - Незалежне від мови перетворення Float/String
Написаний Крістіаном Р. Рейсом, реалізований Густаво Карнейро.
Інші зміни мови¶
Ось усі зміни, які Python 2.4 вносить у основну мову Python.
Додано декоратори для функцій і методів (PEP 318).
Додано вбудовані типи
set()іfrozenset()(PEP 218). Інші нові вбудовані функції включають функціюreversed(seq)(PEP 322).Додано генератор виразів (PEP 289).
Деякі числові вирази більше не повертають значення, обмежені 32 або 64 бітами (PEP 237).
Тепер ви можете взяти дужки навколо списку імен у операторі
from module import names(PEP 328).Метод
dict.update()тепер приймає ті самі форми аргументів, що й конструкторdict. Це включає будь-яке відображення, будь-яке повторення пар ключ/значення та аргументи ключового слова. (Надав Реймонд Геттінгер.)The string methods
ljust(),rjust(), andcenter()now take an optional argument for specifying a fill character other than a space. (Contributed by Raymond Hettinger.)Strings also gained an
rsplit()method that works like thesplit()method but splits from the end of the string. (Contributed by Sean Reifschneider.)>>> 'www.python.org'.split('.', 1) ['www', 'python.org'] 'www.python.org'.rsplit('.', 1) ['www.python', 'org']
Three keyword parameters, cmp, key, and reverse, were added to the
sort()method of lists. These parameters make some common usages ofsort()simpler. All of these parameters are optional.For the cmp parameter, the value should be a comparison function that takes two parameters and returns -1, 0, or +1 depending on how the parameters compare. This function will then be used to sort the list. Previously this was the only parameter that could be provided to
sort().key має бути функцією з одним параметром, яка приймає елемент списку та повертає ключ порівняння для елемента. Потім список сортується за допомогою ключів порівняння. Наступний приклад сортує список без урахування регістру:
>>> L = ['A', 'b', 'c', 'D'] >>> L.sort() # Case-sensitive sort >>> L ['A', 'D', 'b', 'c'] >>> # Using 'key' parameter to sort list >>> L.sort(key=lambda x: x.lower()) >>> L ['A', 'b', 'c', 'D'] >>> # Old-fashioned way >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) >>> L ['A', 'b', 'c', 'D']
The last example, which uses the cmp parameter, is the old way to perform a case-insensitive sort. It works but is slower than using a key parameter. Using key calls
lower()method once for each element in the list while using cmp will call it twice for each comparison, so using key saves on invocations of thelower()method.Для простих ключових функцій і функцій порівняння часто можна уникнути виразу
lambda, використовуючи натомість незв’язаний метод. Наприклад, наведене вище сортування без урахування регістру найкраще записати як:>>> L.sort(key=str.lower) >>> L ['A', 'b', 'c', 'D']
Нарешті, параметр reverse приймає логічне значення. Якщо значення має значення true, список буде відсортовано у зворотному порядку. Замість
L.sort(); L.reverse(), тепер ви можете написатиL.sort(reverse=True).Результати сортування тепер гарантовано стабільні. Це означає, що два записи з однаковими ключами буде повернено в тому ж порядку, у якому вони були введені. Наприклад, ви можете відсортувати список людей за іменами, а потім відсортувати список за віком, у результаті чого вийде список, відсортований за віком, де люди одного віку розташовані в порядку за іменами.
(All changes to
sort()contributed by Raymond Hettinger.)Існує нова вбудована функція
sorted(iterable), яка працює як метод in-placelist.sort(), але її можна використовувати у виразах. Відмінності:вхід може бути будь-яким ітерованим;
новосформовану копію сортують, залишаючи оригінал недоторканим; і
вираз повертає нову відсортовану копію
>>> L = [9,7,8,3,2,4,1,6,5] >>> [10+i for i in sorted(L)] # usable in a list comprehension [11, 12, 13, 14, 15, 16, 17, 18, 19] >>> L # original is left unchanged [9,7,8,3,2,4,1,6,5] >>> sorted('Monty Python') # any iterable may be an input [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y'] >>> # List the contents of a dict sorted by key values >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) >>> for k, v in sorted(colormap.iteritems()): ... print k, v ... black 4 blue 2 green 3 red 1 yellow 5
(Надав Реймонд Геттінгер.)
Integer operations will no longer trigger an
OverflowWarning. TheOverflowWarningwarning will disappear in Python 2.5.Інтерпретатор отримав новий перемикач,
-m, який приймає назву, шукає відповідний модуль уsys.pathі запускає модуль як сценарій. Наприклад, тепер ви можете запустити профайлер Python за допомогоюpython -m profile. (Надав Нік Коглан.)Функції
eval(expr, globals, locals)іexecfile(filename, globals, locals)і операторexecтепер приймають будь-який тип зіставлення для параметра locals. Раніше це мав бути звичайний словник Python. (Надав Реймонд Геттінгер.)The
zip()built-in function anditertools.izip()now return an empty list if called with no arguments. Previously they raised aTypeErrorexception. This makes them more suitable for use with variable length argument lists:>>> def transpose(array): ... return zip(*array) ... >>> transpose([(1,2,3), (4,5,6)]) [(1, 4), (2, 5), (3, 6)] >>> transpose([]) []
(Надав Реймонд Геттінгер.)
Encountering a failure while importing a module no longer leaves a partially initialized module object in
sys.modules. The incomplete module object left behind would fool further imports of the same module into succeeding, leading to confusing errors. (Fixed by Tim Peters.)Noneтепер константа; код, який прив’язує нове значення до іменіNone, тепер є синтаксичною помилкою. (Надав Реймонд Геттінгер.)
Оптимізації¶
The inner loops for list and tuple slicing were optimized and now run about one-third faster. The inner loops for dictionaries were also optimized, resulting in performance boosts for
keys(),values(),items(),iterkeys(),itervalues(), anditeritems(). (Contributed by Raymond Hettinger.)Обладнання для збільшення та скорочення списків було оптимізовано з точки зору швидкості та економії місця. Додавання та видалення зі списків тепер виконується швидше завдяки більш ефективним шляхам коду та менш частому використанню основної системи
realloc(). Розуміння списків також корисне.list.extend()також оптимізовано і більше не перетворює свій аргумент у тимчасовий список перед розширенням базового списку. (Надав Реймонд Геттінгер.)list(),tuple(),map(),filter(), andzip()now run several times faster with non-sequence arguments that supply a__len__()method. (Contributed by Raymond Hettinger.)The methods
list.__getitem__(),dict.__getitem__(), anddict.__contains__()are now implemented asmethod_descriptorobjects rather thanwrapper_descriptorobjects. This form of access doubles their performance and makes them more suitable for use as arguments to functionals:map(mydict.__getitem__, keylist). (Contributed by Raymond Hettinger.)Додано новий код операції,
LIST_APPEND, який спрощує згенерований байт-код для розуміння списків і прискорює їх приблизно на третину. (Надав Реймонд Геттінгер.)Оптимізатор байт-коду peephole було вдосконалено для створення коротшого та швидшого байт-коду; примітно, що отриманий байт-код більш читабельний. (Покращено Раймондом Геттінгером.)
String concatenations in statements of the form
s = s + "abc"ands += "abc"are now performed more efficiently in certain circumstances. This optimization won’t be present in other Python implementations such as Jython, so you shouldn’t rely on it; using thejoin()method of strings is still recommended when you want to efficiently glue a large number of strings together. (Contributed by Armin Rigo.)
Кінцевим результатом оптимізації 2.4 є те, що Python 2.4 виконує тест pystone приблизно на 5% швидше, ніж Python 2.3, і на 35% швидше, ніж Python 2.2. (pystone не є особливо хорошим тестом, але це найбільш часто використовуваний метод вимірювання продуктивності Python. Ваші власні програми можуть продемонструвати більшу чи меншу користь від Python 2.4.)
Нові, покращені та застарілі модулі¶
Як завжди, стандартна бібліотека Python отримала низку вдосконалень і виправлень помилок. Ось неповний список найбільш помітних змін, відсортованих за алфавітом назв модулів. Зверніться до файлу Misc/NEWS у дереві вихідних кодів, щоб отримати більш повний список змін, або перегляньте журнали CVS, щоб отримати всі деталі.
The
asyncoremodule’sloop()function now has a count parameter that lets you perform a limited number of passes through the polling loop. The default is still to loop forever.Модуль
base64тепер має більш повну підтримку RFC 3548 кодування та декодування Base64, Base32 і Base16, включаючи необов’язкове згортання регістру та альтернативні алфавіти. (Надав Баррі Варшау.)Модуль
bisectтепер має базову реалізацію C для покращення продуктивності. (Надав Дмитро Васильєв.)Колекції кодеків Східної Азії CJKCodecs, які підтримує Hye-Shik Chang, були інтегровані у 2.4. Нові кодування:
Китайська (КНР): gb2312, gbk, gb18030, big5hkscs, hz
Китайська (ROC): big5, cp950
- Японська: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,
iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004, shift-jis, shift-jisx0213, shift- jis-2004
Корейська: cp949, euc-kr, johab, iso-2022-kr
Додано деякі інші нові кодування: HP Roman8, ISO_8859-11, ISO_8859-16, PCTP-154 і TIS-620.
The UTF-8 and UTF-16 codecs now cope better with receiving partial input. Previously the
StreamReaderclass would try to read more data, making it impossible to resume decoding from the stream. Theread()method will now return as much data as it can and future calls will resume decoding where previous ones left off. (Implemented by Walter Dörwald.)There is a new
collectionsmodule for various specialized collection datatypes. Currently it contains just one type,deque, a double-ended queue that supports efficiently adding and removing elements from either end:>>> from collections import deque >>> d = deque('ghi') # make a new deque with three items >>> d.append('j') # add a new entry to the right side >>> d.appendleft('f') # add a new entry to the left side >>> d # show the representation of the deque deque(['f', 'g', 'h', 'i', 'j']) >>> d.pop() # return and remove the rightmost item 'j' >>> d.popleft() # return and remove the leftmost item 'f' >>> list(d) # list the contents of the deque ['g', 'h', 'i'] >>> 'h' in d # search the deque True
Several modules, such as the
queueandthreadingmodules, now take advantage ofcollections.dequefor improved performance. (Contributed by Raymond Hettinger.)The
ConfigParserclasses have been enhanced slightly. Theread()method now returns a list of the files that were successfully parsed, and theset()method raisesTypeErrorif passed a value argument that isn’t a string. (Contributed by John Belmonte and David Goodger.)The
cursesmodule now supports the ncurses extensionuse_default_colors(). On platforms where the terminal supports transparency, this makes it possible to use a transparent background. (Contributed by Jörg Lehmann.)The
difflibmodule now includes anHtmlDiffclass that creates an HTML table showing a side by side comparison of two versions of a text. (Contributed by Dan Gass.)The
emailpackage was updated to version 3.0, which dropped various deprecated APIs and removes support for Python versions earlier than 2.3. The 3.0 version of the package uses a new incremental parser for MIME messages, available in theemail.FeedParsermodule. The new parser doesn’t require reading the entire message into memory, and doesn’t raise exceptions if a message is malformed; instead it records any problems in thedefectattribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas Wouters, and others.)The
heapqmodule has been converted to C. The resulting tenfold improvement in speed makes the module suitable for handling high volumes of data. In addition, the module has two new functionsnlargest()andnsmallest()that use heaps to find the N largest or smallest values in a dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)The
httplibmodule now contains constants for HTTP status codes defined in various HTTP-related RFC documents. Constants have names such asOK,CREATED,CONTINUE, andMOVED_PERMANENTLY; use pydoc to get a full list. (Contributed by Andrew Eland.)The
imaplibmodule now supports IMAP’s THREAD command (contributed by Yves Dionne) and newdeleteacl()andmyrights()methods (contributed by Arnaud Mazin).The
itertoolsmodule gained agroupby(iterable[, *func*])function. iterable is something that can be iterated over to return a stream of elements, and the optional func parameter is a function that takes an element and returns a key value; if omitted, the key is simply the element itself.groupby()then groups the elements into subsequences which have matching values of the key, and returns a series of 2-tuples containing the key value and an iterator over the subsequence.Here’s an example to make this clearer. The key function simply returns whether a number is even or odd, so the result of
groupby()is to return consecutive runs of odd or even numbers.>>> import itertools >>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14] >>> for key_val, it in itertools.groupby(L, lambda x: x % 2): ... print key_val, list(it) ... 0 [2, 4, 6] 1 [7] 0 [8] 1 [9, 11] 0 [12, 14] >>>
groupby()is typically used with sorted input. The logic forgroupby()is similar to the Unixuniqfilter which makes it handy for eliminating, counting, or identifying duplicate elements:>>> word = 'abracadabra' >>> letters = sorted(word) # Turn string into a sorted list of letters >>> letters ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] >>> for k, g in itertools.groupby(letters): ... print k, list(g) ... a ['a', 'a', 'a', 'a', 'a'] b ['b', 'b'] c ['c'] d ['d'] r ['r', 'r'] >>> # List unique letters >>> [k for k, g in groupby(letters)] ['a', 'b', 'c', 'd', 'r'] >>> # Count letter occurrences >>> [(k, len(list(g))) for k, g in groupby(letters)] [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
(Надано Hye-Shik Chang.)
itertoolsтакож отримав функцію під назвоюtee(iterator, N), яка повертає N незалежних ітераторів, які копіюють iterator. Якщо N опущено, за замовчуванням буде 2.>>> L = [1,2,3] >>> i1, i2 = itertools.tee(L) >>> i1,i2 (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) >>> list(i1) # Run the first iterator to exhaustion [1, 2, 3] >>> list(i2) # Run the second iterator to exhaustion [1, 2, 3]
Note that
tee()has to keep copies of the values returned by the iterator; in the worst case, it may need to keep all of them. This should therefore be used carefully if the leading iterator can run far ahead of the trailing iterator in a long stream of inputs. If the separation is large, then you might as well uselist()instead. When the iterators track closely with one another,tee()is ideal. Possible applications include bookmarking, windowing, or lookahead iterators. (Contributed by Raymond Hettinger.)A number of functions were added to the
localemodule, such asbind_textdomain_codeset()to specify a particular encoding and a family ofl*gettext()functions that return messages in the chosen encoding. (Contributed by Gustavo Niemeyer.)Some keyword arguments were added to the
loggingpackage’sbasicConfig()function to simplify log configuration. The default behavior is to log messages to standard error, but various keyword arguments can be specified to log to a particular file, change the logging format, or set the logging level. For example:import logging logging.basicConfig(filename='/var/log/application.log', level=0, # Log all messages format='%(levelname):%(process):%(thread):%(message)')
Other additions to the
loggingpackage include alog(level, msg)convenience method, as well as aTimedRotatingFileHandlerclass that rotates its log files at a timed interval. The module already hadRotatingFileHandler, which rotated logs once the file exceeded a certain size. Both classes derive from a newBaseRotatingHandlerclass that can be used to implement other rotating handlers.(Зміни внесено Вінаєм Саджипом.)
Модуль
marshalтепер ділиться вбудованими рядками під час розпакування структури даних. Це може зменшити розмір певних рядків pickle, але основний ефект полягає в тому, щоб зробити файли.pycзначно меншими. (Надав Мартін фон Льовіс.)The
nntplibmodule’sNNTPclass gaineddescription()anddescriptions()methods to retrieve newsgroup descriptions for a single group or for a range of groups. (Contributed by Jürgen A. Erhard.)До модуля
operatorдодано дві нові функції,attrgetter(attr)таitemgetter(index). Обидві функції повертають виклики, які приймають один аргумент і повертають відповідний атрибут або елемент; ці виклики створюють чудові екстрактори даних при використанні зmap()абоsorted(). Наприклад:>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] >>> map(operator.itemgetter(0), L) ['c', 'd', 'a', 'b'] >>> map(operator.itemgetter(1), L) [2, 1, 4, 3] >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
(Надав Реймонд Геттінгер.)
Модуль
optparseбуло оновлено різними способами. Тепер модуль передає свої повідомлення черезgettext.gettext(), що робить можливим інтернаціоналізацію довідки та повідомлень про помилки Optik. Повідомлення довідки для параметрів тепер можуть містити рядок'%default, який буде замінено значенням параметра за замовчуванням. (Надав Грег Ворд.)The long-term plan is to deprecate the
rfc822module in some future Python release in favor of theemailpackage. To this end, theemail.Utils.formatdatefunction has been changed to make it usable as a replacement forrfc822.formatdate(). You may want to write new e-mail processing code with this in mind. (Change implemented by Anthony Baxter.)До модуля
osбуло додано нову функціюurandom(n), яка повертає рядок, що містить n байтів випадкових даних. Ця функція надає доступ до специфічних для платформи джерел випадковості, таких як/dev/urandomу Linux або Windows CryptoAPI. (Надано Тревором Перріном.)Ще одна нова функція:
os.path.lexists(path)повертає true, якщо файл, визначений path, існує, незалежно від того, чи є це символічним посиланням. Це відрізняється від існуючої функціїos.path.exists(path), яка повертає false, якщо path є символічним посиланням, яке вказує на пункт призначення, якого не існує. (Надав Бені Чернявський.)A new
getsid()function was added to theposixmodule that underlies theosmodule. (Contributed by J. Raynor.)Модуль
poplibтепер підтримує POP через SSL. (Надав Гектор Уртубія.)Модуль
profileтепер може профілювати функції розширення C. (Надав Нік Бастін.)The
randommodule has a new method calledgetrandbits(N)that returns a long integer N bits in length. The existingrandrange()method now usesgetrandbits()where appropriate, making generation of arbitrarily large random numbers more efficient. (Contributed by Raymond Hettinger.)Мова регулярних виразів, прийнята модулем
re, була розширена простими умовними виразами, написаними як(?(group)A|B). group — це числовий ідентифікатор групи або назва групи, визначена за допомогою(?P <group> ...)раніше у виразі. Якщо вказана група збігається, шаблон регулярного виразу A буде перевірено на відповідність рядку; якщо група не збігається, замість неї буде використано шаблон B. (Надав Густаво Німейєр.)Модуль
reтакож більше не є рекурсивним, завдяки величезній кількості роботи Густаво Німейєра. У механізмі рекурсивних регулярних виразів певні шаблони призводять до використання великої кількості стеку C, і стек може бути переповнений. Наприклад, якщо ви зіставили 30000-байтовий рядок символівaіз виразом(a|b)+, один кадр стека споживався на символ. Python 2.3 намагався перевірити переповнення стеку та викликати винятокRuntimeError, але певні шаблони могли обійти перевірку, і якщо вам не пощастило, Python міг стати segfault. Механізм регулярних виразів Python 2.4 може без проблем відповідати цьому шаблону.The
signalmodule now performs tighter error-checking on the parameters to thesignal.signal()function. For example, you can’t set a handler on theSIGKILLsignal; previous versions of Python would quietly accept this, but 2.4 will raise aRuntimeErrorexception.Two new functions were added to the
socketmodule.socketpair()returns a pair of connected sockets andgetservbyport(port)looks up the service name for a given port number. (Contributed by Dave Cole and Barry Warsaw.)The
sys.exitfunc()function has been deprecated. Code should be using the existingatexitmodule, which correctly handles calling multiple exit functions. Eventuallysys.exitfunc()will become a purely internal interface, accessed only byatexit.Модуль
tarfileтепер за замовчуванням створює файли tar у форматі GNU. (Надав Ларс Густебель.)The
threadingmodule now has an elegantly simple way to support thread-local data. The module contains alocalclass whose attribute values are local to different threads.import threading data = threading.local() data.number = 42 data.url = ('www.python.org', 80)
Other threads can assign and retrieve their own values for the
numberandurlattributes. You can subclasslocalto initialize attributes or to add methods. (Contributed by Jim Fulton.)Модуль
timeitтепер автоматично вимикає періодичне збирання сміття під час циклу синхронізації. Ця зміна робить послідовні таймінги більш порівнянними. (Надав Реймонд Геттінгер.)Модуль
weakrefтепер підтримує більшу різноманітність об’єктів, включаючи функції Python, екземпляри класів, набори, заморожені набори, дві версії, масиви, файли, сокети та шаблони регулярних виразів. (Надав Реймонд Геттінгер.)The
xmlrpclibmodule now supports a multi-call extension for transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by Brian Quinlan.)The
mpz,rotor, andxreadlinesmodules have been removed.
doctest¶
Модуль doctest зазнав значного рефакторингу завдяки Едварду Лоперу та Тіму Пітерсу. Тестування може бути таким же простим, як запуск doctest.testmod(), але рефакторинг дозволяє налаштувати роботу модуля різними способами
The new DocTestFinder class extracts the tests from a given object’s
docstrings:
def f (x, y):
""">>> f(2,2)
4
>>> f(3,2)
6
"""
return x*y
finder = doctest.DocTestFinder()
# Get list of DocTest instances
tests = finder.find(f)
The new DocTestRunner class then runs individual tests and can produce
a summary of the results:
runner = doctest.DocTestRunner()
for t in tests:
tried, failed = runner.run(t)
runner.summarize(verbose=1)
Наведений вище приклад дає такий вихід:
1 items passed all tests:
2 tests in f
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
DocTestRunner uses an instance of the OutputChecker class to
compare the expected output with the actual output. This class takes a number
of different flags that customize its behaviour; ambitious users can also write
a completely new subclass of OutputChecker.
Засіб перевірки виводу за замовчуванням надає ряд зручних функцій. Наприклад, з прапорцем опції doctest.ELLIPSIS три крапки (...) в очікуваному виведенні відповідають будь-якому підрядку, що полегшує розміщення виходів, які незначно відрізняються:
def o (n):
""">>> o(1)
<__main__.C instance at 0x...>
>>>
"""
Інший спеціальний рядок, <BLANKLINE>, відповідає порожньому рядку:
def p (n):
""">>> p(1)
<BLANKLINE>
>>>
"""
Іншою новою можливістю є створення відображення вихідних даних у стилі відмінностей, вказуючи doctest.REPORT_UDIFF (уніфіковані відмінності), doctest.REPORT_CDIFF (контекстні відмінності) або doctest.REPORT_NDIFF (дельта-стиль) позначки параметрів. Наприклад:
def g (n):
""">>> g(4)
here
is
a
lengthy
>>>"""
L = 'here is a rather lengthy list of words'.split()
for word in L[:n]:
print word
Запустивши тести наведеної вище функції з указаним doctest.REPORT_UDIFF, ви отримаєте такий результат:
**********************************************************************
File "t.py", line 15, in g
Failed example:
g(4)
Differences (unified diff with -expected +actual):
@@ -2,3 +2,3 @@
is
a
-lengthy
+rather
**********************************************************************
Зміни збірки та C API¶
Деякі зміни в процесі збирання Python і в API C:
Додано три нові зручні макроси для типових значень, що повертаються функціями розширення:
Py_RETURN_NONE,Py_RETURN_TRUEіPy_RETURN_FALSE. (Надав Бретт Кеннон.)Another new macro,
Py_CLEAR, decreases the reference count of obj and sets obj to the null pointer. (Contributed by Jim Fulton.)Нова функція,
PyTuple_Pack(N, obj1, obj2, ..., objN), створює кортежі зі списку аргументів змінної довжини об’єктів Python. (Надав Реймонд Геттінгер.)Нова функція,
PyDict_Contains(d, k), реалізує швидкий пошук у словнику без маскування винятків, які виникають під час процесу пошуку. (Надав Реймонд Геттінгер.)The
Py_IS_NANmacro returns 1 if its float or double argument X is a NaN. (Contributed by Tim Peters.)C code can avoid unnecessary locking by using the new
PyEval_ThreadsInitialized()function to tell if any thread operations have been performed. If this function returns false, no lock operations are needed. (Contributed by Nick Coghlan.)Нова функція,
PyArg_VaParseTupleAndKeywords(), така сама, якPyArg_ParseTupleAndKeywords(), але приймаєva_listзамість кількох аргументів. (Надав Грег Чепмен.)A new method flag,
METH_COEXIST, allows a function defined in slots to co-exist with aPyCFunctionhaving the same name. This can halve the access time for a method such asset.__contains__(). (Contributed by Raymond Hettinger.)Тепер Python можна створювати з додатковим профілюванням для самого інтерпретатора, призначеного як допомога людям, які розробляють ядро Python. Надання
--enable-profilingсценарію configure дозволить вам профілювати інтерпретатор за допомогою gprof і надання перемикача--with-tscдозволяє профілювання за допомогою реєстру Time-Stamp-Counter Pentium. Зауважте, що перемикач--with-tscтрохи неправильно названо, тому що функція профілювання також працює на платформі PowerPC, хоча ця архітектура процесора не називає цей регістр «реєстром TSC». (Надав Джеремі Гілтон.)The
tracebackobjecttype has been renamed toPyTracebackObject.
Зміни, що стосуються порту¶
Порт Windows тепер створено під MSVC++ 7.1, а також версію 6. (Надано Мартіном фон Льовісом.)
Перенесення на Python 2.4¶
У цьому розділі перераховано описані раніше зміни, які можуть потребувати змін у вашому коді:
Зрушення вліво та шістнадцяткові/вісімкові константи, які є занадто великими, більше не викликають
FutureWarningі повертають значення, обмежене 32 або 64 бітами; замість цього вони повертають довге ціле число.Integer operations will no longer trigger an
OverflowWarning. TheOverflowWarningwarning will disappear in Python 2.5.The
zip()built-in function anditertools.izip()now return an empty list instead of raising aTypeErrorexception if called with no arguments.You can no longer compare the
dateanddatetimeinstances provided by thedatetimemodule. Two instances of different classes will now always be unequal, and relative comparisons (<,>) will raise aTypeError.dircache.listdir()now passes exceptions to the caller instead of returning empty lists.LexicalHandler.startDTDused to receive the public and system IDs in the wrong order. This has been corrected; applications relying on the wrong order need to be fixed.fcntl.ioctl()тепер попереджає, якщо аргумент mutate пропущений і доречний.Модуль
tarfileтепер за замовчуванням створює файли tar у форматі GNU.Encountering a failure while importing a module no longer leaves a partially initialized module object in
sys.modules.Noneтепер константа; код, який прив’язує нове значення до іменіNone, тепер є синтаксичною помилкою.The
signal.signal()function now raises aRuntimeErrorexception for certain illegal values; previously these errors would pass silently. For example, you can no longer set a handler on theSIGKILLsignal.
Подяки¶
Автор хотів би подякувати наступним особам за пропозиції, виправлення та допомогу з різними чернетками цієї статті: Корай Кан, Хе-Шік Чанг, Майкл Дайк, Реймонд Геттінгер, Браян Херт, Хеміш Лоусон, Фредрік Лунд, Шон Рейфшнайдер, Садруддін Реджеб.