45
私私私私私 Python 私私 vol.2 #nds4 @civic

私の好きなPython構文 vol.2 #nds46

  • Upload
    civicpg

  • View
    1.317

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 私の好きなPython構文 vol.2 #nds46

私の好きな Python 構文 vol.2#nds4 @civic

Page 2: 私の好きなPython構文 vol.2 #nds46

お前だれよ• @civic • NDS 管理者• Python, Java

Page 3: 私の好きなPython構文 vol.2 #nds46

このプレゼンのねらいPython への熱い想いを語って少しでも Python 人口を増やしたい

Page 4: 私の好きなPython構文 vol.2 #nds46

前回の発表おさらい

Page 5: 私の好きなPython構文 vol.2 #nds46

今日帰ってすぐに始められるPython

Page 6: 私の好きなPython構文 vol.2 #nds46

Python の良さ

Page 7: 私の好きなPython構文 vol.2 #nds46

かんたん

Page 8: 私の好きなPython構文 vol.2 #nds46

素直で覚えやすい

Page 9: 私の好きなPython構文 vol.2 #nds46

人気あり

Page 10: 私の好きなPython構文 vol.2 #nds46

プログラミング言語人気ランキング  Top102015 年 8 月 GitHub 発表   http://goo.gl/160eHH

Page 11: 私の好きなPython構文 vol.2 #nds46

Python 人気は?• 4 位  PHP• 5 位  Python• 6 位  CSS• 7 位  C++• 8 位  C#• 9 位  C• 10 位  HTML

• 1 位 JavaScript• 2 位 Java• 3 位 Ruby

Page 12: 私の好きなPython構文 vol.2 #nds46

好きな Python 構文 vol.1

Page 13: 私の好きなPython構文 vol.2 #nds46

デコレータについて紹介しました詳しくはこちらwww.slideshare.net/fbcivic/lt-python-nds45

Page 14: 私の好きなPython構文 vol.2 #nds46

今日話す内容

Page 15: 私の好きなPython構文 vol.2 #nds46

好きな Python 構文 vol.2文字列リテラルwith 文 ( コンテキストマネージャー )

Page 16: 私の好きなPython構文 vol.2 #nds46

文字列リテラル

Page 17: 私の好きなPython構文 vol.2 #nds46

文字列リテラル 基本'Hello' # シングルクォート"World" # ダブルクォート

• 普通な感じ• 変数の展開などはしない

• format 関数• %

Page 18: 私の好きなPython構文 vol.2 #nds46

文字列リテラルのプレフィックス• プレフィックスで意味が変わるb'abc' # バイト列 ≠ 文字列r'\r\n' # raw 文字列(エスケープなし)u' あいう ' # Unicode 文字列 # ( python2 のみ。 3.3 以降では u は不要)リテラルの記号を増やすことなく、アルファベット 1 字で様々なリテラルを表現できてわかりやすい!好き!

Page 19: 私の好きなPython構文 vol.2 #nds46

正規表現リテラルなど不要!• Python では正規表現は言語仕様になく、

標準ライブラリ。正規表現リテラルはない。• JavaScript の正規表現リテラル// \n にマッチするか ?test_string.match(/\n/);

// \ にマッチするか ?test_string.match(/\\/);

Page 20: 私の好きなPython構文 vol.2 #nds46

Java の場合• 同様に正規表現が標準ライブラリで提供される Java

の場合• Java の場合// \n にマッチするか ? test_string.matches("\\n");

// \ にマッチするか ?test_string.matches("\\\\");

文字列でパターンを表現するので、正規表現としてのエスケープ文字と文字列リテラルのエスケープが重なる

Page 21: 私の好きなPython構文 vol.2 #nds46

Python の場合• raw 文字列を使えばさほど問題ない• Python の場合// \n にマッチするか ? re.match(r"\n", test_string)

// \ にマッチするか ?re.match(r"\\", test_string);

raw 文字列  r""→ エスケープ無効な文字列リテラル言語仕様に正規表現リテラルを含めなくてもライブラリで十分!→シンプル!

Page 22: 私の好きなPython構文 vol.2 #nds46

文字列リテラルのプレフィックス• 記号を使わずに文字列リテラルをたくさん表現できる

"abc" 文字列r"\n" raw 文字列

b"abc" バイト列u" あいう " Unicode 文字列 (Python2)

f"Hello! {name}" 変数展開できるやつ(Python3.6 予定 )

Page 23: 私の好きなPython構文 vol.2 #nds46

余談ですが

Page 24: 私の好きなPython構文 vol.2 #nds46

Python2 と 3 の文字列(闇)

Page 25: 私の好きなPython構文 vol.2 #nds46

Python2 の文字列• Python2 の文字列リテラルはバイト列

なんらかのエンコードの文字列→バイト列• 文字単位で数えたりするには、

なんらかのエンコードのバイト列では扱いにくい→ Unicode 文字列もあるよ

"Hello" # 文字列(バイト列)u"Hello" # Unicode 文字列

Page 26: 私の好きなPython構文 vol.2 #nds46

混ぜるな危険!!Python2 の文字列 vs Unicode 文字列

UnicodeDecodeError: 'ascii' 〜

Page 27: 私の好きなPython構文 vol.2 #nds46

安心してください  Python3 では自然な感じに

Python2 意味 Python3

"abc" バイト列 b"abc"

u"abc" Unicode 文字列 "abc"

• 自然になったが互換性が ...

• u" " リテラルはもう付けなくてもいいから不要!

Page 28: 私の好きなPython構文 vol.2 #nds46

Python3.0 ではu"" は廃止!

Page 29: 私の好きなPython構文 vol.2 #nds46

Python2 の u"" が全部使えなくなった!互換性ヤバい!

Page 30: 私の好きなPython構文 vol.2 #nds46

ゴメン!やっぱり Python3.3 からu"" 復活させるわ!

Page 31: 私の好きなPython構文 vol.2 #nds46

お分かりいただけただろうか?

Page 32: 私の好きなPython構文 vol.2 #nds46

えっと ...Python の文字列リテラルが好きだって話だった

Page 33: 私の好きなPython構文 vol.2 #nds46

このプレゼンのねらい(再)Python への熱い想いを語って少しでも Python 人口を増やしたい

Page 34: 私の好きなPython構文 vol.2 #nds46

文字列リテラル まとめ• 3 になってからは自然な感じ• プレフィックスで表現できて便利

• 記号よりわかりやすい• Python3.6 で新しい文字列リテラル f"Hello {name}" # フォーマット文字列https://docs.python.org/3.6/whatsnew/3.6.html#pep-498-formatted-string-literals

Page 35: 私の好きなPython構文 vol.2 #nds46

今日 2 つめの好きな構文

Page 36: 私の好きなPython構文 vol.2 #nds46

with 文コンテキストマネージャー

Page 37: 私の好きなPython構文 vol.2 #nds46

with 文• コンテキストを扱う便利な構文(雑)with open('hoge.txt', 'w') as f: f.write('Hello World')

# with ブロックを抜けるとファイルを close

「ファイルを開いた」というコンテキストで処理を記述

Page 38: 私の好きなPython構文 vol.2 #nds46

Java でいうところの• try-with-resource

try (Write w = new FileWriter(path)) { // ファイル処理}

• Java の場合は AutoClosable インターフェースを実装したオブジェクトを返す

• Python の場合はコンテキストマネージャー型

Page 39: 私の好きなPython構文 vol.2 #nds46

コンテキストマネージャー型• __enter__ と __exit__ メソッドをもつオブジェクトclass Hoge(): def __enter__(self): print("Enter") def __exit__(self, exc_type, exc_value, traceback): print("Exit")

with Hoge(): print("Hello")

# Enter# Hello# Exit

コードブロックの入り口と出口

Page 40: 私の好きなPython構文 vol.2 #nds46

もう少しかんたんに

Page 41: 私の好きなPython構文 vol.2 #nds46

contextlib モジュール• コンテキストマネージャー型のクラスつくるの

メンドクサ → contextlib でお手軽に@contextlib.contextmanagerdef テストすっぞ (): print("test setup') yield print("test teardown")

with テストすっぞ (): print(" テスト中 ")

# test setup# テスト中# test teardown

Page 42: 私の好きなPython構文 vol.2 #nds46

contextlib.contextmanager• 前処理・後処理が簡単に記述できた!

• リソースの close だけでなく定型的な前処理・後処理に

• yield は関数の途中で保留して戻るようなやつ• yield についてはまた別の機会に

Page 43: 私の好きなPython構文 vol.2 #nds46

まとめ

Page 44: 私の好きなPython構文 vol.2 #nds46

まとめ• 好きな Python 構文として2つ挙げました

• 文字列リテラル(のプレフィックス )• わかりやすくて表現の幅が広がる

• with 文(コンテキストマネージャー)• 前処理・後処理がきれいに書ける

Page 45: 私の好きなPython構文 vol.2 #nds46

ご静聴ありがとうございました