python kyoto study
TRANSCRIPT
![Page 1: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/1.jpg)
1
Python3のススメ
Python京都勉強会2011-06-11
![Page 2: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/2.jpg)
2
お前、誰よ
● 稲田 尚也 (@naoina)● 株式会社SOBAプロジェクト所属
● Python歴はたぶん9ヶ月ぐらい (bitbucket調べ)● http://www.naniyueni.org/● 最近は音ゲー(REFLEC BEAT、jubeat)をやってた
りします
![Page 3: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/3.jpg)
3
アジェンダ
● Pythonとは● 特徴● 2系と3系● 機能紹介
![Page 4: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/4.jpg)
4
アジェンダ
● Pythonとは● 特徴● 2系と3系● 機能紹介
![Page 5: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/5.jpg)
5
Pythonとは
![Page 6: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/6.jpg)
6
Pythonとは
● 動的型付けのオブジェクト指向プログラミング言語● 作者はGuido van Rossum氏● 名前の由来は「空飛ぶモンティ・パイソン」から
● ニシキヘビからじゃないよ!● Googleの公式言語の1つ
![Page 7: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/7.jpg)
7
アジェンダ
● Pythonとは● 特徴● 2系と3系● 機能紹介
![Page 8: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/8.jpg)
8
特徴
![Page 9: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/9.jpg)
9
特徴
● インタプリタ● オフサイドルール (インデントが構文の一部)● ダックタイピング● batteries included
![Page 10: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/10.jpg)
10
特徴
インタプリタ
![Page 11: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/11.jpg)
11
特徴
インタプリタ
http://ja.wikipedia.org/wiki/インタプリタ
![Page 12: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/12.jpg)
12
特徴
インタプリタ
% cat juicy_karaage.pysay = "No.1"print(say)% python juicy_karaage.pyNo.1
![Page 13: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/13.jpg)
13
特徴
オフサイドルール
![Page 14: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/14.jpg)
14
特徴
オフサイドルール
http://ja.wikipedia.org/wiki/オフサイドルール
![Page 15: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/15.jpg)
15
特徴
オフサイドルール
def f(): print("running f()")
def main(): f()
if __name__ == "__main__": main()
![Page 16: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/16.jpg)
16
特徴
ダックタイピング
![Page 17: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/17.jpg)
17
特徴
ダックタイピング
http://ja.wikipedia.org/wiki/ダックタイピング
![Page 18: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/18.jpg)
18
特徴
ダックタイピング
もしそれがアヒルのように歩きアヒルのように鳴くのならば
それはアヒルである
![Page 19: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/19.jpg)
19
特徴
ダックタイピング
class Duck: def say(self): print("quack")
class Mami: def say(self): print("tiro quack!")
def quack(animal): animal.say()
quack(Duck())quack(Mami())
ソース
![Page 20: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/20.jpg)
20
特徴
ダックタイピング
class Duck: def say(self): print("quack")
class Mami: def say(self): print("tiro quack!")
def quack(animal): animal.say()
quack(Duck())quack(Mami())
% python quack.pyquacktiro quack!
ソース 実行結果
![Page 21: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/21.jpg)
21
マミさんはアヒルになったのだ・・・
![Page 22: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/22.jpg)
22
特徴
batteries included
![Page 23: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/23.jpg)
23
特徴
batteries included
http://docs.python.org/py3k/library/
2系は http://docs.python.org/library/
![Page 24: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/24.jpg)
24
特徴
batteries included● ネットワーク (http(s)、ftp、smtp、pop、socket)● XMLパーサー (DOM、SAX、XPath)● 圧縮 (gzip、bzip2、zip、tar)● SQLite● CSVパーサー● ユニットテスト● デバッガ● AST● email、json、thread、multiprocess、etc...
![Page 25: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/25.jpg)
25
アジェンダ
● Pythonとは● 特徴● 2系と3系● 機能紹介
![Page 26: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/26.jpg)
26
2系と3系
![Page 27: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/27.jpg)
27
2系と3系
Python2.x
![Page 28: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/28.jpg)
28
2系と3系
Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け
![Page 29: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/29.jpg)
29
2系と3系
Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け
Python3.x
![Page 30: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/30.jpg)
30
2系と3系
Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け
Python3.x– 最新リリースは3.2– ほとんど意識することなくマルチバイトを扱える– 新しい機能が使える
![Page 31: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/31.jpg)
31
2系と3系
マルチバイト文字列
Python2.xまでは
>>> u"這いよる" + "混沌"Traceback (most recent call last): File "<stdin>", line 1, in <module>UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)>>>
# coding: utf-8print "名状しがたいバールのようなもの"
![Page 32: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/32.jpg)
32
2系と3系
マルチバイト文字列
Python3.xでは
>>> "生ける" + "炎"'生ける炎'>>>
print("名状しがたいPerlのようなもの")
![Page 33: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/33.jpg)
33
2系と3系
マルチバイト文字列
Python3.xでは
>>> "生ける" + "炎"'生ける炎'>>>
print("名状しがたいPerlのようなもの")
UTF-8で書けばほぼ問題は出ない!
![Page 34: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/34.jpg)
34
アジェンダ
● Pythonとは● 特徴● 2系と3系● 機能紹介
![Page 35: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/35.jpg)
35
機能紹介
![Page 36: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/36.jpg)
36
機能紹介
![Page 37: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/37.jpg)
37
ここが便利だよPython3
![Page 38: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/38.jpg)
38
ここが便利だよPython3
Python3.xにしかない便利機能を紹介
![Page 39: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/39.jpg)
39
ここが便利だよPython3
● 集合リテラル● 内包表記● 複数with文● 順序付き辞書
![Page 40: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/40.jpg)
40
ここが便利だよPython3
集合リテラル
![Page 41: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/41.jpg)
41
ここが便利だよPython3
集合リテラル
>>> {1, 2, 3, 2, 1}{1, 2, 3}>>>>>> {"madoka", "ao", "mami", "anko", "hmhm", "mami"}{'mami', 'madoka', 'hmhm', 'ao', 'anko'}>>>
![Page 42: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/42.jpg)
42
ここが便利だよPython3
集合リテラル
>>> {1, 2, 3, 2, 1}{1, 2, 3}>>>>>> {"madoka", "ao", "mami", "anko", "hmhm", "mami"}{'mami', 'madoka', 'hmhm', 'ao', 'anko'}>>>
set([1, 2, 3, 2, 1])みたいなのと同じ!
![Page 43: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/43.jpg)
43
ここが便利だよPython3
内包表記
![Page 44: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/44.jpg)
44
ここが便利だよPython3
内包表記
>>> [i for i in range(10)][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>
※Python2.xでも出来る
![Page 45: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/45.jpg)
45
ここが便利だよPython3
内包表記
>>> {i: i * 2 for i in range(5)}{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}>>>
![Page 46: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/46.jpg)
46
ここが便利だよPython3
内包表記
辞書も内包表記出来る!
>>> {i: i * 2 for i in range(5)}{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}>>>
![Page 47: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/47.jpg)
47
ここが便利だよPython3
内包表記
辞書も内包表記出来る!
>>> {i: i * 2 for i in range(5)}{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}>>>
さらに・・・
![Page 48: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/48.jpg)
48
ここが便利だよPython3
内包表記
>>> a = ["知ってる?", "知らない", "ミストルティンキック"]>>> b = ["キックじゃない", "知ってる?", "知らない"]>>> {x for x in a + b}{'知らない', 'ミストルティンキック', '知ってる?', 'キックじゃない'}>>>
![Page 49: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/49.jpg)
49
ここが便利だよPython3
内包表記
集合も内包表記出来る!
>>> a = ["知ってる?", "知らない", "ミストルティンキック"]>>> b = ["キックじゃない", "知ってる?", "知らない"]>>> {x for x in a + b}{'知らない', 'ミストルティンキック', '知ってる?', 'キックじゃない'}>>>
![Page 50: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/50.jpg)
50
ここが便利だよPython3
複数with文
![Page 51: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/51.jpg)
51
ここが便利だよPython3
複数with文
with open("in.txt") as infile: with open("out.txt", "w") as outfile: outfile.write(infile.read())
Python2.x
![Page 52: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/52.jpg)
52
ここが便利だよPython3
複数with文
with open("in.txt") as infile: with open("out.txt", "w") as outfile: outfile.write(infile.read())
with open("in.txt") as infile, open("out.txt", "w") as outfile: outfile.write(infile.read())
Python2.x
Python3.x
![Page 53: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/53.jpg)
53
ここが便利だよPython3
順序付き辞書
![Page 54: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/54.jpg)
54
ここが便利だよPython3
順序付き辞書
>>> d = {}>>> d["Linux"] = "Arch">>> d["BSD"] = "Free">>> d["Mac"] = "Lion">>> d["Windows"] = "sucks">>> for k, v in d.items():... print(k, v)... BSD FreeWindows sucksMac LionLinux Arch>>>
組み込み型の辞書
![Page 55: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/55.jpg)
55
ここが便利だよPython3
順序付き辞書
>>> d = {}>>> d["Linux"] = "Arch">>> d["BSD"] = "Free">>> d["Mac"] = "Lion">>> d["Windows"] = "sucks">>> for k, v in d.items():... print(k, v)... BSD FreeWindows sucksMac LionLinux Arch>>>
>>> import collections>>> d = collections.OrderedDict()>>> d["Linux"] = "Arch">>> d["BSD"] = "Free">>> d["Mac"] = "Lion">>> d["Windows"] = "sucks">>> for k, v in d.items():... print(k, v)... Linux ArchBSD FreeMac LionWindows sucks>>>
組み込み型の辞書 順序付き辞書
![Page 56: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/56.jpg)
56
ここまでであることに気がついた方
![Page 57: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/57.jpg)
57
Python3.xにしかない便利機能を紹介すると言ったな
![Page 58: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/58.jpg)
58
あれは嘘だ
![Page 59: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/59.jpg)
59
● 集合リテラル● 内包表記● 複数with文● 順序付き辞書
![Page 60: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/60.jpg)
60
● 集合リテラル● 内包表記● 複数with文● 順序付き辞書
実はすべてPython2.7にバックポートされているという事実
![Page 61: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/61.jpg)
61
● ただし、2.7はあくまで3.xへの移行を容易にするのが目的
![Page 62: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/62.jpg)
62
● ただし、2.7はあくまで3.xへの移行を容易にするのが目的
● 2系の最終リリース (予定)
![Page 63: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/63.jpg)
63
Python3.2
![Page 64: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/64.jpg)
64
Python3.2
http://docs.python.org/py3k/whatsnew/3.2.html
![Page 65: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/65.jpg)
65
Python3.2
● ElementTree 1.3 (xml.etree.ElementTree)● LRU cache decorator (functools.lru_cache)● Command line option parser (argparse)● Unicode 6.0.0● etc...
![Page 66: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/66.jpg)
66
まとめ
![Page 67: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/67.jpg)
67
まとめ
● 今から始めるならPython3● 新しい機能を使いたいならPython3● マルチバイト周りで苦労したくないならPython3● 現実的にはPython2と3の併用
![Page 68: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/68.jpg)
68
質疑応答
![Page 69: Python Kyoto study](https://reader034.vdocuments.pub/reader034/viewer/2022042715/559c95891a28ab5f298b4657/html5/thumbnails/69.jpg)
69
ありがとうございました