This article is the 16th day article of Python Part 2 Advent Calendar 2015.
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
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')]))
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 ...)
About small changes. I will summarize it easily.
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.
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.
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 deprecatedNewly created as a static method
bytes.maketrans()
bytearray.maketrans()
Can be used.
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 integerIt 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.
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
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.
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).
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)
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.
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 !!
#
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.'
str.format_map ()
It's like format, dictionary version.
Python3.2
>>> '{msg1} {msg2}!!'.format_map({"msg1":"Hello", "msg2":"World"})
'Hello World!!'
-q
optionPython3.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 runtimehasattr ()
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.
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.
release ()
methodPython3.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.
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?
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.
$ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'
It can also be specified with the -W
option.
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'>
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 backPython3.2
>>> callable(max)
True
>>> callable(1)
False
A function that can check if it can be called.
It seems that it has become.
Python3.2 -> Python3.3
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.
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.
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?
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
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.
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'
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'
--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.
This would mean that PIP will be introduced as standard.
That's right.
However, you can use the following methods if you want.
os.get_inheritable()
, os.set_inheritable()
os.get_handle_inheritable()
, os.set_handle_inheritable()
socket.socket.get_inheritable()
, socket.socket.set_inheritable()
--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. .. ..
Use the ʻasync def` syntax to create coroutine functions.
Python3.5
>>> async def cofunc():
... return 'test'
...
In addition, the following syntax has been added.
async for
async with
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 the
await ()` method on any object.
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.])
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}
%
format in bytes and bytearrayPython3.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
...
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
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.
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
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
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