Changes from Python 3.0 to Python 3.5

Introduction

This article is the 16th day article of Python Part 2 Advent Calendar 2015.

Caution

This time,

--New module --Improved module --Deprecated module

I will not touch on, but I will only introduce the parts that are close to the essence such as Python grammar and primitive types. (Since there are some touched parts, please skip in that case ...)

Towards the end, I don't have much time and it gets messy, but please forgive me.

Python3.0 -> Python3.1

Ordered dictionary

Introduced the collections.OrderedDict class. It's like a dictionary that remembers the order in which it was inserted.

Python3.1


from collections import OrderedDict

>>> {2:"a", 1:"b", 3:"c"}.items()
dict_items([(1, 'b'), (2, 'a'), (3, 'c')])
>>> 
>>> OrderedDict([(2, "a"), (1, "b"), (3, "c")]).items()
ItemsView(OrderedDict([(2, 'a'), (1, 'b'), (3, 'c')]))

Format specifier for 3-digit separator

The built-in functions format () and str.format () now allow you to use format specifiers for 3-digit separators. You can specify it with a comma. Supports int, float, complex, decimal.Decimal.

Python3.1


>>> format(123456789, ',d')
'123,456,789'
>>> format(123456789, ',f')
'123,456,789.000000'
>>> format(123456789, ',.2f')
'123,456,789.00'
>>> 
>>> "{0:,d}".format(123456789)
'123,456,789'
>>> "{0:,f}".format(123456789)
'123,456,789.000000'
>>> "{0:,.2f}".format(123456789)
'123,456,789.00'

No, I'm ashamed to know that I can specify the format for the first time. .. I was wondering if there was something to say, but I didn't check it.

Since format () is written as format (value [, format_spec]), I understand that you can specify the format, but The str.format () didn't know where to look. For the built-in type, str.format (* args, ** kwargs) I don't understand at all because it only says.

While saying that, I managed to find here. (It is a secret that there was a link directly under str.format ...)

Other language changes

About small changes. I will summarize it easily.

Run directories and zip archive files directly

The directory containing \ _ \ _ main \ _ \ _. Py and the zip archive can be run directly by passing its name to the interpreter.

Umm. It's hard to understand.

__main__.py


print("Hello World!!")

Create a file like this and save it with the path python_test / __ main__.py. Then try making a zip.

$ zip -j python_test.zip python_test/__main__.py

Check if __main__.py is in the root.

$ unzip -l python_test.zip
Archive:  python_test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       23  2015-12-16 12:34   __main__.py
---------                     -------
       23                     1 file

Then run!

$ #Run from folder
$ python python_test
Hello World!!
$ #Run from archive
$ python python_test.zip
Hello World!!

Well, you can do it like this.

Bit \ _length method added to int type

Python3.1


>>> n = 37
>>> bin(n)
'0b100101'
>>> n.bit_length()
6
>>> n = 2**123-1
>>> n.bit_length()
123

Yes. The number of digits in binary comes out like this.

The fields of the format () string are automatically numbered.

Python3.1


>>> "{} {}!!".format("Hello", "World")
'Hello World!!'

I also learned this for the first time ... It's convenient!

string.maketrans () is deprecated

Newly created as a static method

Can be used.

The syntax of the with statement allows you to pass multiple context managers in one statement

Python3.1


>>> class WithTest:
...  def __init__(self, msg):
...   self.msg = msg
...  
...  def __enter__(self):
...   print("enter: "+self.msg)
...   return self
...  
...  def __exit__(self, exc_type, exc_value, traceback):
...   print("exit: "+self.msg)
...   return True
... 
>>> with WithTest("hoge") as hoge, WithTest("fuga") as fuga:
...  pass
... 
enter: hoge
enter: fuga
exit: fuga
exit: hoge

You can write with statements at once without nesting them. The movement is the same as the nested movement.

This has deprecated contextlib.nested ().

round (x, n) now returns an integer if x is an integer

It seems that it used to return floating point numbers.

Python3.1


>>> round(1234, -2)
1200
>>> round(1234., -2)
1200.0
>>> round(1234, 2)
1234

If you pass an integer, it will be returned as an integer.

Uses David Gay's algorithm to display floating point

It remains the title. Floating point is displayed using a new algorithm.

Python3.0


>>> 1.1
1.1000000000000001

Python3.1


>>> 1.1
1.1

It was difficult for me to talk any more ~ Please tell me!

Python3.1 -> Python3.2

argparse command line analysis module

An upgraded version of the argparse module from optparse has been added.

Supports positional arguments, subcommands, option validation, etc.

The official version of this module is detailed, so if you want to know more about it, please visit here.

Dictionary-based settings for logging

There was a method of setting the logging module by calling a function or reading a file using the format of ConfigParser.

As a flexible way to make this setting, you can now make settings using a dictionary. Set information to logging.config.dictConfig () It can be set by passing the stored dictionary.

You can set it flexibly by parsing a file written in JSON or YAML and passing it to a method.

Click here for details on the logging module [http://docs.python.jp/3/library/logging.html#module-logging).

concurrent.futures module

The concurrent.futures module has been added. This module provides a high level interface for callable objects that can be run asynchronously.

You can use the ThreadPoolExecutor to run it in a thread, or you can use the ProcessPoolExecutor to run it in another process.

Click here for details on the concurrent.futures module [http://docs.python.jp/3/library/concurrent.futures.html)

PYC repository directory

If the .pyc file of the cache file generated by Python is operated by another version of the interpreter, the existing cache cannot be used, so a new cache file is created and overwritten.

To deal with this, the file with the Python version added to the end of the file name is now in the \ _ \ _ pycache \ _ \ _ directory.

Python3.2


$ touch mymodule.py
$ python -c "import mymodule"
$ ls
__pycache__  mymodule.py
$ ls __pycache__
mymodule.cpython-32.pyc

The cache of the imported module is created in the \ _ \ _ pycache \ _ \ _ folder with the file name with the version.

.So file tagged with ABI version

Shared objects are also Python implementation methods (CPython, Jython, PyPy, etc.) + major and minor version numbers + build flags You can now place multiple files with the name.

Python Web Server Gateway Interface v1.0.1

This PEP clarifies how the WSGI protocol handles bytes and text problems. PEP is for byte strings used in request and response bodies Distinguish between so-called native strings used in request / response headers and metadata.

Native strings are always str, but can be converted to bytes using Latin-1 encoding and are limited to code points between U + 0000 and U + 00FF. These strings are used in the key and value in the environment variable dictionary and in the response header and status of start \ _response (). That is, you must use ISO-8891-1 characters or RFC 2047 MIME encoding.

Server implementers of CGI-WSGI routes and other CGI-style protocols require users to use native strings to access environment variables across different platforms.

To fill this gap, the wsgiref module has a new wsgiref.handlers.read_environ () that converts CGI variables from os.environ to native strings and returns a new dictionary.

Well, I tried my best to read English, but I didn't really understand what was important (laughs).

The introduction is long if wsgiref.handlers.read_environ () wanted to say. Was it just this ...?

Those who understand Help !!

Other language changes

# Added to the format specifiers offormat ()andstr.format ()

--For binary, octal, and hexadecimal outputs for integers, add the prefixes '0b', '0o', and '0x', respectively. --For floating point numbers, complex numbers, and decimal numbers, the decimal point character is included even if there is no number after the decimal point character.

Python3.2


>>> format(10, 'o')
'12'
>>> format(10, '#o')
'0o12'
>>> format(10, 'b')
'1010'
>>> format(10, '#b')
'0b1010'
>>> format(10, '.0f')
'10'
>>> format(10, '#.0f')
'10.'

Addition of str.format_map ()

It's like format, dictionary version.

Python3.2


>>> '{msg1} {msg2}!!'.format_map({"msg1":"Hello", "msg2":"World"})
'Hello World!!'

Addition of -q option

Python3.2


$ python
Python 3.2.6 (default, Dec  9 2015, 17:42:33) 
[GCC 5.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python -q
>>> 

By adding the -q option, it is possible to hide the version information when the interpreter is started.

hasattr () now throws an exception at runtime

hasattr () callsgetattr ()to see if an exception is detected. Previously, the behavior was False if any exception was detected, but now exceptions other than AttributeError are passed through.

Python3.1


>>> class C:
...  @property
...  def f(self):
...   return 1//0
...  @property
...  def g(self):
...   return 0
... 
>>> c = C()
>>> hasattr(c, 'f')
False
>>> hasattr(c, 'g')
True
>>> hasattr(c, 'h')
False

Python3.2


>>> class C:
...  @property
...  def f(self):
...   return 1//0
...  @property
...  def g(self):
...   return 0
... 
>>> c = C()
>>> hasattr(c, 'f')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in f
ZeroDivisionError: integer division or modulo by zero
>>> hasattr(c, 'g')
True
>>> hasattr(c, 'h')
False

Zero Division Error does not appear in Python 3.1. At first glance it looks like it has attributes, but with an exception the result is False.

There are exceptions in Python 3.2. However, there is no AttributeError and the result is False.

The behavior of str () in complex and float is now the same as repr ()

Python3.1


>>> import math
>>> repr(math.pi)
'3.141592653589793'
>>> str(math.pi)
'3.14159265359'

Python3.2


>>> import math
>>> repr(math.pi)
'3.141592653589793'
>>> str(math.pi)
'3.141592653589793'

In 3.1, the result of str () is a little short.

The memoryview object now has a release () method

Python3.2


>>> with memoryview(b'abc') as v:
...  print(v.tolist())
...
[97, 98, 99]

Resources are released by calling the release () method. In addition, __enter__ () and __exit__ () have also been implemented so you can use the with statement.

You can now remove names that appear as free variables in nested blocks

Python3.1


>>> def outer(x):
...  def inner():
...   return x
...  inner()
...  del x
...
SyntaxError: can not delete variable 'x' referenced in nested scope
>>> 

Python3.2


>>> def outer(x):
...  def inner():
...   return x
...  inner()
...  del x
...
>>> 

What should I do with this? I think it would be helpful if you gave me an error. For the time being, this kind of code in 2.6 seems to work.

Python3.2


>>> def f():
...  def print_error():
...   print(e)
...  try:
...   pass
...  except Exception as e:
...   print_error()
>>> 

What I learned here is that e is implicitly del. Sure, I want this to work, but I'm the only one who thinks I shouldn't write this kind of code, right?

The internal struct sequence tool is now made up of tuple subclasses

Python3.2


>>> isinstance(sys.version_info, tuple)
True

ʻOs.stat (), time.gmtime (), sys.version_info` will return it. You can treat it like a named tuple.

Easily control Warning with the PYTHONWARNINGS environment variable

$ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'

It can also be specified with the -W option.

Added Resource Warning

It is disabled by default and must be enabled with the -W option to display it.

Python3.2


$ python -q -Wdefault
>>> f = open("foo", "wb")
>>> del f
__main__:1: ResourceWarning: unclosed file <_io.BufferedWriter name='foo'>

Range object now supports index, count and slice

Python3.2


>>> r=range(0,100,2)
range(0, 100, 2)
>>> r.count(10)
1
>>> r.index(10)
5
>>> r[10]
20
>>> r[0:5]
range(0, 10, 2)

It seems that it can have a value larger than sys.maxsize.

callable () built-in functions are back

Python3.2


>>> callable(max)
True
>>> callable(1)
False

A function that can check if it can be called.

Non-ASCII characters can be used in the path to the module folder

It seems that it has become.

Python3.2 -> Python3.3

About virtual environment

Added a venv module for programmatic access and a pyvenv script for command line access.

I've only used virtualenv, should I use it? Also, when I hit pyvenv, it says venv in usage. ..

Python3.3


$ pyvenv
usage: venv [-h] [--system-site-packages] [--symlinks] [--clear] [--upgrade]
            ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

It seems that this command module is benefiting from the integration with the interpreter core, so it may be better to use it here.

Implicit namespace package

You no longer need to put \ _ \ _ init \ _ \ _. Py in the package directory.

Python3.2


$ ls -R
hoge

./hoge:
fuga.py
$ python -c "import hoge.fuga"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named hoge.fuga

Python3.3


$ ls -R
hoge

./hoge:
fuga.py
$ python -c "import hoge.fuga"
$ 

It seems that Python 3.3 can be read because there is no error.

Reworking OS, IO exception hierarchy

All exception types are now OSError. (Other names are left as aliases)

Also, it is now ready to supplement specific error conditions. Instead of looking at a specific constant for the errno attribute, you can supplement the appropriate OSError derived class.

ConnectionError has a finely derived class.

This is an official example

Python3.2


from errno import ENOENT, EACCES, EPERM

try:
    with open("document.txt") as f:
        content = f.read()
except IOError as err:
    if err.errno == ENOENT:
        print("document.txt file is missing")
    elif err.errno in (EACCES, EPERM):
        print("You are not allowed to read document.txt")
    else:
        raise

Python3.3


try:
    with open("document.txt") as f:
        content = f.read()
except FileNotFoundError:
    print("document.txt file is missing")
except PermissionError:
    print("You are not allowed to read document.txt")

I can now write. Python3.3 is smart! Is Python 3.2 a C language?

Delegation syntax to subgenerator

A yield from statement has been added.

A generator can delegate its processing to another generator.

Python3.3


>>> def g():
...  yield from [1,2,3]
...  yield from [-1,-2,-3]
...
>>> gen = g()
>>> list(gen)
[1, 2, 3, -1, -2, -3]

Also, the yield from statement can return the final value specified in the return statement.

Python3.3


>>> def g():
...  yield 1
...  yield 2
...  yield 3
...  return 0
...
>>> def f():
...  data = yield from g()
...  print('return value: ' + str(data))
...
>>> gen = f()
>>> next(gen)
1
>>> next(gen)
2
>>> next(gen)
3
>>> next(gen)
return value: 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Suppression of exception context

You can suppress the exception context by specifying None in the raise from statement.

Python3.3


>>> def f():
...  raise Exception('in function')
...
>>> def g1():
...  try:
...   f()
...  except Exception as e:
...   raise Exception('test1')
...
>>> def g2():
...  try:
...   f()
...  except Exception as e:
...   raise Exception('test2') from None
...
>>> g1()
Traceback (most recent call last):
  File "<stdin>", line 3, in g1
  File "<stdin>", line 2, in f
Exception: in function

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in g1
Exception: test1
>>> g2()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in g2
Exception: test2

Exception context is suppressed in g2.

Explicit unicode literal

You can now prefix the string with ʻu` again.

Python3.2


>>> u"test"
  File "<stdin>", line 1
    u"test"
          ^
SyntaxError: invalid syntax

Python3.3


>>> u"test"
'test'

Qualified name of a function in a class

You can get the path from the top level with the \ _ \ _ qualname \ _ \ _ attribute.

Python3.3


>>> class C:
...  class D:
...   def f():
...    pass
...
>>> C.D.__name__
'D'
>>> C.D.__qualname__
'C.D'
>>> C.D.f.__qualname__
'C.D.f'

Built-in functions and types

--Added'x' mode to ʻopen (). Fails if the file already exists --Added flush keyword argument to print (). If true, the stream will be forced to flush. --Hash randomization is enabled by default. See ʻobject .__ hash __ () and PYTHON HASHSEED. --Added casefold () method to str type.

Python3.3 -> Python3.4

3.3 to 3.4 are short because there are no new grammar features.

Explicit bootstrapping of PIP during Python installation

This would mean that PIP will be introduced as standard.

Newly created file descriptors cannot be inherited

That's right.

However, you can use the following methods if you want.

Other language changes

--min () and max () can return the value specified by the keyword argument if there are no elements to iterate. --bytes.join () and byte array.join () accept arbitrary buffer objects as arguments. --The int constructor accepts any object with __index__ ().

Python3.4 -> Python3.5

As for 3.5, I haven't touched it so much, so it's a shallow introduction & I don't have time, so it's short and easy. .. ..

Add async and await syntax

Use the ʻasync def` syntax to create coroutine functions.

Python3.5


>>> async def cofunc():
...  return 'test'
...

In addition, the following syntax has been added.

These syntaxes can only be used in coroutine functions defined by ʻasync def`.

You can use the ʻawaitstatement inside the Koluitin function. The await statement can suspend the Kolu people's execution until the resulting value is valid. It can be used by defining theawait ()` method on any object.

Inlay operator for matrix multiplication

You can multiply the matrix by using @. It can be used by implementing __matmul __ (), __ rmatmul __ (), __imatmul __ () in the object.

It seems to be implemented in numpy. From the official doc.

Python3.5


>>> import numpy
>>> x = numpy.ones(3)
>>> x
array([ 1., 1., 1.])
>>> m = numpy.eye(3)
>>> m
array([[ 1., 0., 0.],
       [ 0., 1., 0.],
       [ 0., 0., 1.]])
>>> x @ m
array([ 1., 1., 1.])

Unpacking extension

You can use * for iterator objects and ** for dictionaries.

Python3.5


>>> print(*[1,2], 3, *[4])
1 2 3 4
>>> def f(a, b, c, d):
...  print(a,b,c,d)
...
>>> f(**{'a': 1, 'd': 4}, **{'c': 3}, **{'b': 2})
1 2 3 4
>>> *range(4), 4
(0, 1, 2, 3, 4)
>>> [*range(4), 4]
[0, 1, 2, 3, 4]
>>> {*range(4), 4}
{0, 1, 2, 3, 4}
>>> {**{'x': 3}, 'y': 4}
{'x': 3, 'y': 4}

Support for % format in bytes and bytearray

Python3.5


>>> b'test: %d' % 1
b'test: 1'

% b does not support Unicode. But you can use % a.

Python3.5


>>> b'Hello %b!' % b'World'
b'Hello World!'
>>> b'Hello %b!' % 'World'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'
>>> b'Hello %a!' % 'World'
b"Hello 'World'!"
>>> b'price: %a' % '10€'
b"price: '10\\u20ac'"

Type Hints

Type Hints has been introduced. I will write in detail in another article at a later date. ..

Specify using function annotation.

Python3.5


>>> def f(num: int) -> int:
...  return num+1
...

ʻOs.scandir ()` function added

A function that iterates the contents of a directory. It seems to be early. Let's use it.

ls -a
.         ..        .hidefile empty     hoge

Python3.5


>>> import os
>>> for entry in os.scandir('.'):
...  if not entry.name.startswith('.') and entry.is_file():
...   print(entry.name)
...
empty

Re-execution of system call in EINTR

Previously in Python, it was necessary to either ignore ʻInterruptedError` or provide a mechanism to restart after catching.

From Python 3.5, system calls in EINTR will be automatically re-executed.

Changes in the handling of Stop Iteration in the generator

Previously, the occurrence of Stop Iteration in the generator would be raised as is.

Python3.4


>>> def gen():
...  next(iter([]))
...  yield
...
>>> next(gen())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in gen
StopIteration

Importing the generator_stop of __future__ will throw an exception as a RuntimeError.

Python3.5


>>> from __future__ import generator_stop
>>> def gen():
...  next(iter([]))
...  yield
...
>>> next(gen())
Traceback (most recent call last):
  File "<stdin>", line 2, in gen
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration

Approximate equality comparison

math.isclose () has been added.

Python3.5


>>> import math
>>> a = 5.
>>> b = 4.99998
>>> math.isclose(a, b, rel_tol=1e-5)
True
>>> math.isclose(a, b, rel_tol=1e-6)
False
>>> math.isclose(a, b, abs_tol=0.00003)
True
>>> math.isclose(a, b, abs_tol=0.00001)
False

At the end

3.4 and 3.5 are pretty rough. .. ..

Since it is a minor update, there are few grammatical updates. If you include bug fixes, improvements, and module additions, the amount will be like a demon, so I omitted it this time. However, I'm curious about the asyncio module added in 3.4 and the typing module added in 3.5, so I'll write an article later.

I touched Python 3.3 and didn't mention much about the new features of later versions, so I'm sorry that the explanation is vague (´ ・ ω ・ `)

If you are interested, please see Official doc! !! (See you again

Tomorrow is @ fx-kirin.

Let's have a good Python3 life! !!

Recommended Posts

Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Call Matlab from Python to optimize
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
How to access wikipedia from python
Python to switch from another language
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
Updated to Python 2.7.9
Sum from 1 to 10
sql from python
MeCab from Python
"Backport" to python 2
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Send a message from Python to Slack
Private Python handbook (updated from time to time)
I want to use jar from python
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Send a message from Slack to a Python server
Use thingsspeak from python
Edit Excel from Python to create a PivotTable
Touch MySQL from Python 3
How to open a web browser from python
Operate Filemaker from Python
How to install Python
Use fluentd from python
Study from Python Hour7: How to use classes
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
Python from or import
Transition from WSL1 to WSL2
I want to email from Gmail using Python.
Rewrite Python2 code to Python3 (2to3)
[Python] I want to manage 7DaysToDie from Discord! 1/3
From file to graph drawing in Python. Elementary elementary
Use MySQL from Python
How to install python
[Python] How to read data from CIFAR-10 and CIFAR-100
python decorator to retry
[python] Create table from pandas DataFrame to postgres
Run python from excel