いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Pythonメモ-59 (dockerでpython 3.7のdataclassを試す) (dataclass, PEP 526, docker, rc-alpine, python 3.7.0a4)

概要

前回、python 3.7 で追加される Data Classes についての情報をメモしたので

devlights.hatenablog.com

ついでにちょっと試してみようと思います。

docker で試す

でも、python 3.7 はまだアルファ版です。いきなり自分の環境に入れたりするのは嫌ですよね。

とかいって、仮想環境をvagrantとかで作ってもいいのですが、今回は docker でやります。

やっぱ楽なので。

Docker Hub の Python ページ見ると rc-stretch, rc-slim, rc-alpineのタグがありますね。

今回は、ちょっとお試ししたいだけなので、サイズが小さい rc-alpine でいきます。

$ docker run -it --rm python:rc-alpine
Unable to find image 'python:rc-alpine' locally
rc-alpine: Pulling from library/python
ff3a5c916c92: Pull complete 
471170bb1257: Pull complete 
3576b9290ad6: Pull complete 
682dced8659d: Pull complete 
e357f9ed8ef3: Pull complete 
Digest: sha256:1a96023decb61dfe2b57ccfd0ec06f51d750999ea659d22a15a6a71760e26dfe
Status: Downloaded newer image for python:rc-alpine
Python 3.7.0a4 (default, Jan 10 2018, 05:17:21) 
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

バッチリ。便利な時代になりましたねぇ。。

てことで、ちょっと遊んでみる。

Data Classes 試してみる

Data Classes については、前回の記事にて分かりやすい情報公開してくださっている方々のページ見るのが一番です。

以下、自分用の軽いメモです。

>>> # Data Class 機能を使うためのimport
>>> from dataclasses import dataclass, astuple, asdict
>>> # データクラスを定義してみる
>>> @dataclass
... class C:
...     x: int
...     y: str
...     z: int = -1
... 
>>> # OK. できた
>>> # インスタンス作ってみる
>>> c = C(1, 'helloworld')  # 最後の引数はデフォルト値指定している
>>> c
C(x=1, y='helloworld', z=-1)
>>> # 値変更してみる
>>> c.x = 100
>>> c
C(x=100, y='helloworld', z=-1)
>>> # もう一個作ってみる
>>> c2 = C(1, 'helloworld', -1)
>>> c2
C(x=1, y='helloworld', z=-1)
>>> # 同じ?
>>> c == c2
False
>>> c.x = 1
>>> # 同じ?
>>> c == c2
True
>>> # 同じフィールド数を持つ namedtuple 作ってみる
>>> from collections import namedtuple
>>> D = namedtuple('D', ['x', 'y', 'z'])
>>> d = D(1, 'helloworld', -1)
>>> d
D(x=1, y='helloworld', z=-1)
>>> # 同じ?
>>> c == d
False
>>> # GOOD!!
>>> # namedtupleの場合、たまたま同じフィールドを持っていると同じになってしまう
>>> E = namedtuple('E', ['a', 'b', 'c'])
>>> e = E(1, 'helloworld', -1)
>>> d == e
True
>>> c == e
False
>>> # namedtuple は名前の通り、後から値を変更できない
>>> e.a = 100  # ERROR: AttributeError: can't set attribute
>>> # namedtupleは、名前の通り tuple なので、同じ値の tuple と比較すると一致する
>>> e == (1, 'helloworld', -1)
True
>>> # dataclass はそうならない
>>> c == (1, 'helloworld', -1)
False
>>> # tuple にするには astuple を使う
>>> astuple(c)
(1, 'helloworld', -1)
>>> # dict にするには、asdict を使う
>>> asdict(c)
{'x': 1, 'y': 'helloworld', 'z': -1}
>>> astuple(c) == (1, 'helloworld', -1)
True
>>> asdict(c) == dict(x=1, y='helloworld', z=-1)
True
>>> # 満足
>>> quit()
$
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              rc-alpine           4df58c01b1b7        3 weeks ago         85.8MB
$ docker rmi python:rc-alpine
Untagged: python:rc-alpine
Untagged: python@sha256:1a96023decb61dfe2b57ccfd0ec06f51d750999ea659d22a15a6a71760e26dfe
Deleted: sha256:4df58c01b1b78339129e8849e5ea81db0ce10812df6dcf6b23f0ff5156de9769
Deleted: sha256:b5aeb063aa825d47e012044aff4138520a47ac9594bb1c907e87f7aa2cc50a35
Deleted: sha256:05da2d3b33e649ed13df5c80d59c83e197e0537a016e3c346cc7e660adecd35b
Deleted: sha256:2569558d6fc2c80ba0b3d5d3ae355bfda0edc868d3f7313dd3a8863d80ff42b0
Deleted: sha256:bfb4cbe300f3859ff67b589214f50b43a47db4186217d576a619a4171eed9735
Deleted: sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215

過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。