64
世界一言語トークス by Python Yoshiori SHOJI 20091214日月曜日

(裏)世界一言語トークス by Python

Embed Size (px)

Citation preview

Page 1: (裏)世界一言語トークス by Python

世界一言語トークスby PythonYoshiori SHOJI

2009年12月14日月曜日

Page 2: (裏)世界一言語トークス by Python

世界一言語トークスby PythonYoshiori SHOJI

2009年12月14日月曜日

Page 3: (裏)世界一言語トークス by Python

注意このプレゼンに登場する

考え方は大変 Python 的ではありません。

ご注意ください。

2009年12月14日月曜日

Page 4: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 5: (裏)世界一言語トークス by Python

自 己紹 介

2009年12月14日月曜日

Page 6: (裏)世界一言語トークス by Python

庄司嘉

織2009年12月14日月曜日

Page 7: (裏)世界一言語トークス by Python

庄司嘉

織2009年12月14日月曜日

Page 8: (裏)世界一言語トークス by Python

vaJ a第 一 言 語

2009年12月14日月曜日

Page 9: (裏)世界一言語トークス by Python

vaJ a第 一 言 語

2009年12月14日月曜日

Page 10: (裏)世界一言語トークス by Python

u assa kar b

.

2009年12月14日月曜日

Page 11: (裏)世界一言語トークス by Python

u assa kar b

.

2009年12月14日月曜日

Page 12: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 13: (裏)世界一言語トークス by Python

インデントがブロックってキモイ!!

2009年12月14日月曜日

Page 14: (裏)世界一言語トークス by Python

確かにPython はインデントを用いたブロック構造なので

def hoge(): print 'foo' print 'bar'

2009年12月14日月曜日

Page 15: (裏)世界一言語トークス by Python

確かにPython はインデントを用いたブロック構造なので

def hoge(): print 'foo' print 'bar'

def hoge(): print 'foo' print 'bar'

2009年12月14日月曜日

Page 16: (裏)世界一言語トークス by Python

確かにPython はインデントを用いたブロック構造なので

def hoge(): print 'foo' print 'bar'

def hoge(): print 'foo' print 'bar'X2009年12月14日月曜日

Page 17: (裏)世界一言語トークス by Python

でも!!

2009年12月14日月曜日

Page 18: (裏)世界一言語トークス by Python

λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ

lambda があるじゃないか!!

2009年12月14日月曜日

Page 19: (裏)世界一言語トークス by Python

でも!?

2009年12月14日月曜日

Page 20: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 21: (裏)世界一言語トークス by Python

「文」な困ったちゃん

2009年12月14日月曜日

Page 22: (裏)世界一言語トークス by Python

「文」な困ったちゃん• Python では代入が文•ちょw (再)代入出来ないwwまぁ、関数型っぽくて良いか

2009年12月14日月曜日

Page 23: (裏)世界一言語トークス by Python

「文」な困ったちゃん• Python では代入が文•ちょw (再)代入出来ないwwまぁ、関数型っぽくて良いか

• print が文•ちょw デバッグ出来ねぇwwww

2009年12月14日月曜日

Page 24: (裏)世界一言語トークス by Python

というか

•処理が続けて書けないw• foo() bar() って文じゃんwwww

2009年12月14日月曜日

Page 25: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 26: (裏)世界一言語トークス by Python

比較式• if 文とかで使うアレif( A && B ) とか if A and B

• A and B (&&)A を評価して真だったら B を評価

• A or B ( || )A を評価して偽だったら B を評価

2009年12月14日月曜日

Page 27: (裏)世界一言語トークス by Python

irb(main):014:0> p 1 && 2 2=> nilirb(main):015:0> p 1 || 2 1=> nil

比較式

>>> print 1 and 22>>> print 1 or 21

2009年12月14日月曜日

Page 28: (裏)世界一言語トークス by Python

irb(main):014:0> p 1 && 2 2=> nilirb(main):015:0> p 1 || 2 1=> nil

比較式

>>> print 1 and 22>>> print 1 or 21

これじゃね?

2009年12月14日月曜日

Page 29: (裏)世界一言語トークス by Python

Python の真偽値Python では None、 False、ゼロ(数値)、 空配列、空辞書、

及びユーザ定義クラスで __nonzero__() や __len__() が

数値 zero または False を返す場合は偽として扱われます。

それ以外は真として扱われます。

2009年12月14日月曜日

Page 30: (裏)世界一言語トークス by Python

複数の処理を式に

2009年12月14日月曜日

Page 31: (裏)世界一言語トークス by Python

foo() and bar()複数の処理を式に

2009年12月14日月曜日

Page 32: (裏)世界一言語トークス by Python

foo() and bar()左辺が"真"だと右辺が実行される

式になった!!

複数の処理を式に

2009年12月14日月曜日

Page 33: (裏)世界一言語トークス by Python

foo() and bar()左辺が"真"だと右辺が実行される

式になった!!でも

左辺の評価結果が常に"真"にならないと困る

複数の処理を式に

2009年12月14日月曜日

Page 34: (裏)世界一言語トークス by Python

foo() and bar()左辺が"真"だと右辺が実行される

式になった!!でも

左辺の評価結果が常に"真"にならないと困る

[ foo() ] and bar()

複数の処理を式に

2009年12月14日月曜日

Page 35: (裏)世界一言語トークス by Python

foo() and bar()左辺が"真"だと右辺が実行される

式になった!!でも

左辺の評価結果が常に"真"にならないと困る

[ foo() ] and bar()[ ] で囲む事によって要素数 1 のリストになり

常に"真"になる

複数の処理を式に

2009年12月14日月曜日

Page 36: (裏)世界一言語トークス by Python

さらに

2009年12月14日月曜日

Page 37: (裏)世界一言語トークス by Python

[ foo() ] and bar()

さらに

2009年12月14日月曜日

Page 38: (裏)世界一言語トークス by Python

[ foo() ] and bar()

ただの連続処理なら一気に初期化しちゃえばいい

さらに

2009年12月14日月曜日

Page 39: (裏)世界一言語トークス by Python

[foo(), bar(), hoge(),]

[ foo() ] and bar()

ただの連続処理なら一気に初期化しちゃえばいい

さらに

2009年12月14日月曜日

Page 40: (裏)世界一言語トークス by Python

if 文を式にif A : foo() bar()

2009年12月14日月曜日

Page 41: (裏)世界一言語トークス by Python

[ foo() ] and [ bar() ] if A else True

if 文を式にif A : foo() bar()

2009年12月14日月曜日

Page 42: (裏)世界一言語トークス by Python

[ foo() ] and [ bar() ] if A else True2.5から三項演算子が出来たのでそれを使う

if 文を式にif A : foo() bar()

2009年12月14日月曜日

Page 43: (裏)世界一言語トークス by Python

if 文を式に(2)

2009年12月14日月曜日

Page 44: (裏)世界一言語トークス by Python

if 文を式に(2)[[ foo() ] and [ bar() ] if A else True ]

2009年12月14日月曜日

Page 45: (裏)世界一言語トークス by Python

if 文を式に(2)[[ foo() ] and [ bar() ] if A else True ]

これ自体も[ ] で囲む事によって常に"真"になる

2009年12月14日月曜日

Page 46: (裏)世界一言語トークス by Python

[A and [ foo() ] and [ bar() ]]

2.5以前は三項演算子が無かったので

if 文を式に(2)[[ foo() ] and [ bar() ] if A else True ]

これ自体も[ ] で囲む事によって常に"真"になる

2009年12月14日月曜日

Page 47: (裏)世界一言語トークス by Python

for文を式に

2009年12月14日月曜日

Page 48: (裏)世界一言語トークス by Python

Python にはリスト内包表記が有る!!

for文を式に

2009年12月14日月曜日

Page 49: (裏)世界一言語トークス by Python

Python にはリスト内包表記が有る!!

for文を式に

for x in range(10) : foo( x )

2009年12月14日月曜日

Page 50: (裏)世界一言語トークス by Python

Python にはリスト内包表記が有る!!

for文を式に

for x in range(10) : foo( x )

[ foo( x ) for x in range(10) ]

2009年12月14日月曜日

Page 51: (裏)世界一言語トークス by Python

関数の返り値

2009年12月14日月曜日

Page 52: (裏)世界一言語トークス by Python

def hoge( data ): return [foo()] and bar( data )

関数の返り値

2009年12月14日月曜日

Page 53: (裏)世界一言語トークス by Python

def hoge( data ): return [foo()] and bar( data )最初に return 書けば最後に評価した値が返る

関数の返り値

2009年12月14日月曜日

Page 54: (裏)世界一言語トークス by Python

def hoge( data ): return [foo()] and bar( data )最初に return 書けば最後に評価した値が返る

関数の返り値

lambda 化hoge = lambda data :([foo()] and bar( data ) )

2009年12月14日月曜日

Page 55: (裏)世界一言語トークス by Python

tipsprint 文は

代入はリストの append や辞書の__setitem__を使用する。グローバルな名前空間globals()、ローカルの名前空間locals()、そしてオブジェクトxの名前空間x.__dict__

2009年12月14日月曜日

Page 56: (裏)世界一言語トークス by Python

tipsimport sys;[sys.stdout.write(data)]print 文は

代入はリストの append や辞書の__setitem__を使用する。グローバルな名前空間globals()、ローカルの名前空間locals()、そしてオブジェクトxの名前空間x.__dict__

globals().__setitem__("setitem", globals().__setitem__)

2009年12月14日月曜日

Page 57: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 58: (裏)世界一言語トークス by Python

2009年12月14日月曜日

Page 59: (裏)世界一言語トークス by Python

実際にやってみた(某美人時計の画像ぶっこぬき編)

import sys,os,urllib,time;[[[globals().__setitem__('url','http://www.bijint.com/jp/img/photo/%02d%02d.jpg' % (i, j))] and [globals().__setitem__('file',open(os.path.basename(url), 'wb'))] and [file.write(urllib.urlopen(url).read())] and [file.close()] and [time.sleep(5)] for j in range(60)] for i in range(24)]

2009年12月14日月曜日

Page 60: (裏)世界一言語トークス by Python

実際にやってみた(fizbuzネタ編)

import sys;[globals().__setitem__("setitem", globals().__setitem__)] and [setitem('isSun',lambda : sys.platform.find('java') > -1)] and [setitem('p',lambda data :(isSun() and ([globals().__setitem__('java',__import__('java'))] and [java.lang.System.out.write(java.lang.String(data).getBytes("utf-8"))] and [sys.stdout.write('\n')]) or [sys.stdout.write(data + '\n')]))] and [setitem('fizbuz',lambda i :([setitem('fiz','Fiz')] and [setitem('buz','Buz')] and [setitem('ret','')] and [isSun() and ([setitem('fiz','ふぃずぅ')] and [setitem('buz','ばずぅ')])] and [i % 3 == 0 and setitem('ret', globals().get('ret') + fiz)] and [i % 5 == 0 and setitem('ret', globals().get('ret') + buz)] and [len(globals().get('ret')) == 0 and setitem('ret', str(i))] and ret))] and [p(fizbuz(i)) for i in xrange(20)]

2009年12月14日月曜日

Page 61: (裏)世界一言語トークス by Python

全部式だからPythonなのに

フリーレイアウト!!

2009年12月14日月曜日

Page 62: (裏)世界一言語トークス by Python

インデントがブロックってキモイ!!

2009年12月14日月曜日

Page 63: (裏)世界一言語トークス by Python

インデントがブロックってキモイ!!

別にインデント使わないで書けばいじゃん

2009年12月14日月曜日

Page 64: (裏)世界一言語トークス by Python

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

2009年12月14日月曜日