タグ

ブックマーク / atsuoishimoto.hatenablog.com (5)

  • The Zen of Python 解題 - 前編 - atsuoishimoto's diary

    The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In

    The Zen of Python 解題 - 前編 - atsuoishimoto's diary
    Nyoho
    Nyoho 2020/10/10
    The Zen of Python
  • Python 3.8 の概要 (その3) - Pickle protocol 5 with out-of-band data - atsuoishimoto's diary

    Pythonでは、複雑なデータの交換や保管する場合、よく Pickleモジュール が使われます。Pickleはデータを外部に出力可能な形式に変換してファイルに変換したり、サーバと通信して送信したりします。 Pythonのconcurrent.futures や multiprocessing を使って並列処理を行う場合も、プロセス間のデータ交換に Pickle が使われています。 PEP-574 Pickle protocol 5 with out-of-band data Pickleは汎用的なデータフォーマットを定義していて、データを作成したハードウェアと異なるアーキテクチャのハード上で読み込んでも、ただしく元のデータを再現できるようになっています。 しかし、現在ではPickleの使い方は多様化しており、そういった汎用的なデータフォーマットだけでは効率的にデータの転送や保管を行えないこ

    Python 3.8 の概要 (その3) - Pickle protocol 5 with out-of-band data - atsuoishimoto's diary
    Nyoho
    Nyoho 2019/09/08
  • Python 3.8 の概要 (その2) - Positional-only parameters - atsuoishimoto's diary

    Python 3.0 以降では、関数を定義するときに、キーワード専用引数 を指定できるようになりました。 def func(a, b, *, c=1, d=2): return a+b+c+d こんなのですね。引数のリストに * がある関数を呼び出すとき、* の後ろにある引数の値は、かならずキーワード引数として指定しなければいけません。 ↑の関数だと、引数 c はキーワード引数で指定すればちゃんと動きます。 >>> func(1, 2, c=10) 15 しかし、キーワードなしで呼び出すとエラーになります。 >>> func(1, 2, 10) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: func() takes 2 positional arguments but 3

    Python 3.8 の概要 (その2) - Positional-only parameters - atsuoishimoto's diary
    Nyoho
    Nyoho 2019/09/06
  • Python 3.8 の概要 (その1) - Assignment expressions - atsuoishimoto's diary

    古来、Pythonでは「代入は文であるべき!」と一貫して主張してきました。 C言語などでは、代入は足し算や掛け算と同じ、値を計算する「式」で、たとえば a = (b=100) / 2; と書くと、b には 100 を代入し、a に 100/2=50 を代入します。1+1 は 2 という値になる 式 ですが、b=100 も同様に値が 100 となる 式 なのです。 Pythonでは、代入は式ではないので、こういう書き方はできません。 Pythonの代入は、足し算などの演算子の仲間ではなく、if や for のような制御文の仲間で、あまり自由な書き方は出来ないのです。 Python FAQ では、その理由として Python の式中での代入を許さない理由は、この構造によって起こる、他の言語ではありがちで見つけづらいバグです: if (x = 0) { // error handling } e

    Python 3.8 の概要 (その1) - Assignment expressions - atsuoishimoto's diary
    Nyoho
    Nyoho 2019/09/06
  • Python 3.6の概要 (その1 - f文字列) - atsuoishimoto's diary

    f文字列 これまで、Pythonで文字列に変数を埋め込む方式にはいくつかあったが、ついに式を文字列中に直接記述できるようになった。 (PEP 498 -- Literal String Interpolation) 式を埋め込んだ文字列はf文字列(formatted string:フォーマット済み文字列)と呼ばれ、raw文字列の r と同じように、先頭に f を指定する f文字列は通常の文字列と同じようも使えるが、文字列内の {} で囲まれた部分は、Pythonの式として評価し、その結果を文字列として出力する。 >>> f'hello' # 式を含まないf文字列 'hello' >>> f'100+1={100+1}' # 式を評価 '100+1=101' >>> order={'spam':100, 'ham':200} >>> f'spam: {order["spam"]}, ham:

    Python 3.6の概要 (その1 - f文字列) - atsuoishimoto's diary
    Nyoho
    Nyoho 2016/12/26
  • 1