python kyoto study

69
1 Python3のススメ Python京都勉強会 2011-06-11

Upload: naoya-inada

Post on 08-Jul-2015

2.974 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Python Kyoto study

1

Python3のススメ

Python京都勉強会2011-06-11

Page 2: Python Kyoto study

2

お前、誰よ

● 稲田 尚也 (@naoina)● 株式会社SOBAプロジェクト所属

● Python歴はたぶん9ヶ月ぐらい (bitbucket調べ)● http://www.naniyueni.org/● 最近は音ゲー(REFLEC BEAT、jubeat)をやってた

りします

Page 3: Python Kyoto study

3

アジェンダ

● Pythonとは● 特徴● 2系と3系● 機能紹介

Page 4: Python Kyoto study

4

アジェンダ

● Pythonとは● 特徴● 2系と3系● 機能紹介

Page 5: Python Kyoto study

5

Pythonとは

Page 6: Python Kyoto study

6

Pythonとは

● 動的型付けのオブジェクト指向プログラミング言語● 作者はGuido van Rossum氏● 名前の由来は「空飛ぶモンティ・パイソン」から

● ニシキヘビからじゃないよ!● Googleの公式言語の1つ

Page 7: Python Kyoto study

7

アジェンダ

● Pythonとは● 特徴● 2系と3系● 機能紹介

Page 8: Python Kyoto study

8

特徴

Page 9: Python Kyoto study

9

特徴

● インタプリタ● オフサイドルール (インデントが構文の一部)● ダックタイピング● batteries included

Page 10: Python Kyoto study

10

特徴

インタプリタ

Page 11: Python Kyoto study

11

特徴

インタプリタ

http://ja.wikipedia.org/wiki/インタプリタ

Page 12: Python Kyoto study

12

特徴

インタプリタ

% cat juicy_karaage.pysay = "No.1"print(say)% python juicy_karaage.pyNo.1

Page 13: Python Kyoto study

13

特徴

オフサイドルール

Page 14: Python Kyoto study

14

特徴

オフサイドルール

http://ja.wikipedia.org/wiki/オフサイドルール

Page 15: Python Kyoto study

15

特徴

オフサイドルール

def f(): print("running f()")

def main(): f()

if __name__ == "__main__": main()

Page 16: Python Kyoto study

16

特徴

ダックタイピング

Page 17: Python Kyoto study

17

特徴

ダックタイピング

http://ja.wikipedia.org/wiki/ダックタイピング

Page 18: Python Kyoto study

18

特徴

ダックタイピング

もしそれがアヒルのように歩きアヒルのように鳴くのならば

それはアヒルである

Page 19: Python Kyoto study

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

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

21

マミさんはアヒルになったのだ・・・

Page 22: Python Kyoto study

22

特徴

batteries included

Page 23: Python Kyoto study

23

特徴

batteries included

http://docs.python.org/py3k/library/

2系は http://docs.python.org/library/

Page 24: Python Kyoto study

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

25

アジェンダ

● Pythonとは● 特徴● 2系と3系● 機能紹介

Page 26: Python Kyoto study

26

2系と3系

Page 27: Python Kyoto study

27

2系と3系

Python2.x

Page 28: Python Kyoto study

28

2系と3系

Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け

Page 29: Python Kyoto study

29

2系と3系

Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け

Python3.x

Page 30: Python Kyoto study

30

2系と3系

Python2.x– 最新リリースは2.7.1– マルチバイトを意識しないとハマる– 現状のサードパーティ製ライブラリはほとんど2.x向け

Python3.x– 最新リリースは3.2– ほとんど意識することなくマルチバイトを扱える– 新しい機能が使える

Page 31: Python Kyoto study

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

32

2系と3系

マルチバイト文字列

Python3.xでは

>>> "生ける" + "炎"'生ける炎'>>>

print("名状しがたいPerlのようなもの")

Page 33: Python Kyoto study

33

2系と3系

マルチバイト文字列

Python3.xでは

>>> "生ける" + "炎"'生ける炎'>>>

print("名状しがたいPerlのようなもの")

UTF-8で書けばほぼ問題は出ない!

Page 34: Python Kyoto study

34

アジェンダ

● Pythonとは● 特徴● 2系と3系● 機能紹介

Page 35: Python Kyoto study

35

機能紹介

Page 36: Python Kyoto study

36

機能紹介

Page 37: Python Kyoto study

37

ここが便利だよPython3

Page 38: Python Kyoto study

38

ここが便利だよPython3

Python3.xにしかない便利機能を紹介

Page 39: Python Kyoto study

39

ここが便利だよPython3

● 集合リテラル● 内包表記● 複数with文● 順序付き辞書

Page 40: Python Kyoto study

40

ここが便利だよPython3

集合リテラル

Page 41: Python Kyoto study

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

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

43

ここが便利だよPython3

内包表記

Page 44: Python Kyoto study

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

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

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

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

48

ここが便利だよPython3

内包表記

>>> a = ["知ってる?", "知らない", "ミストルティンキック"]>>> b = ["キックじゃない", "知ってる?", "知らない"]>>> {x for x in a + b}{'知らない', 'ミストルティンキック', '知ってる?', 'キックじゃない'}>>>

Page 49: Python Kyoto study

49

ここが便利だよPython3

内包表記

集合も内包表記出来る!

>>> a = ["知ってる?", "知らない", "ミストルティンキック"]>>> b = ["キックじゃない", "知ってる?", "知らない"]>>> {x for x in a + b}{'知らない', 'ミストルティンキック', '知ってる?', 'キックじゃない'}>>>

Page 50: Python Kyoto study

50

ここが便利だよPython3

複数with文

Page 51: Python Kyoto study

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

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

53

ここが便利だよPython3

順序付き辞書

Page 54: Python Kyoto study

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

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

56

ここまでであることに気がついた方

Page 57: Python Kyoto study

57

Python3.xにしかない便利機能を紹介すると言ったな

Page 58: Python Kyoto study

58

あれは嘘だ

Page 59: Python Kyoto study

59

● 集合リテラル● 内包表記● 複数with文● 順序付き辞書

Page 60: Python Kyoto study

60

● 集合リテラル● 内包表記● 複数with文● 順序付き辞書

実はすべてPython2.7にバックポートされているという事実

Page 61: Python Kyoto study

61

● ただし、2.7はあくまで3.xへの移行を容易にするのが目的

Page 62: Python Kyoto study

62

● ただし、2.7はあくまで3.xへの移行を容易にするのが目的

● 2系の最終リリース (予定)

Page 63: Python Kyoto study

63

Python3.2

Page 64: Python Kyoto study

64

Python3.2

http://docs.python.org/py3k/whatsnew/3.2.html

Page 65: Python Kyoto study

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

66

まとめ

Page 67: Python Kyoto study

67

まとめ

● 今から始めるならPython3● 新しい機能を使いたいならPython3● マルチバイト周りで苦労したくないならPython3● 現実的にはPython2と3の併用

Page 68: Python Kyoto study

68

質疑応答

Page 69: Python Kyoto study

69

ありがとうございました