Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
私の知らないPythonの世界
Search
massyuu
December 20, 2017
Technology
1
510
私の知らないPythonの世界
pyfukuoka #3
at 2017/12/20
in Fukuoka City
massyuu
December 20, 2017
Tweet
Share
More Decks by massyuu
See All by massyuu
僕が仕事を楽しむメンタルモデル
massyuu
0
210
自分のTwitter利用を振り返ってみる
massyuu
0
83
Vue×Nativeの開発を簡単にしたかったのです
massyuu
0
380
Authから始めるFirebaseの第一歩
massyuu
1
1.3k
RealWorldHTTPを読んでみた
massyuu
1
540
Pythonのアプリ開発にElectronという選択肢
massyuu
1
2.3k
Other Decks in Technology
See All in Technology
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
350
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
250
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
120
マイクロサービスにおける容易なトランザクション管理に向けて
scalar
0
140
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
170
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
260
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
非機能品質を作り込むための実践アーキテクチャ
knih
5
1.5k
2024年にチャレンジしたことを振り返るぞ
mitchan
0
140
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
5分でわかるDuckDB
chanyou0311
10
3.2k
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Building an army of robots
kneath
302
44k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Optimizing for Happiness
mojombo
376
70k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Become a Pro
speakerdeck
PRO
26
5k
Making the Leap to Tech Lead
cromwellryan
133
9k
A designer walks into a library…
pauljervisheath
204
24k
Transcript
ࢲͷΒͳ͍ 1ZUIPOͷੈք !NBTTZVV 1Z'VLVPLB ʙσίϨʔλʔΛͦ͑ͯʙ
ࣗݾհ !NBTTZVV 1ZUIPOझຯͰͬͯ·͢ ͔Β͋͛ΛࡇΓਸΊΔΤϯδχϟʔ ϑϩϯτ͔ΒόοΫΤϯυ·Ͱ͘ઙ͘ 4PTJBM UXJUUFSNBTT@TTO DPOOQBTTɿNBTTZVV
ࠓͷ͓ 1ZUIPOΛ͍࢝Ίͯ ʮ΄΄͏ʯ ͱࢥͬͨ͜ͱΛ 5JQTతʹ·ͱΊ·ͨ͠
ࠓͷ͓ 1ZUIPOΛ͍࢝Ίͯ ʮ΄΄͏ʯ ͱࢥͬͨ͜ͱΛ 5JQTతʹ·ͱΊ·ͨ͠
ࠓͷ͓ ߴ֊ؔͬͯͳΜ͡Ό͍ʁ σίϨʔλʔͬͯͳΜ͡Ό͍ʁ ͱ͍͏͓Ͱ͢
͓ॻ͖ ɾpMUFSɺNBQɺSFEVDF ɾߴ֊ؔ ɾσίϨʔλʔ ɾδΣωϨʔλʔ ɾแදهͱ͔ͱ͔
ҙ ࠓճ1ZUIPOͰͷ͓Ͱ͢ ʢ1ZUIPOͱॻ͖ํ͕ҟͳΔ߹͕͋Γ·͢ʣ
pMUFS NBQ ΠςϨʔλΛ͢ͱɺͦΕͧΕରԠͨ͠ॲཧΛߦͬͯ ΠςϨʔλΛฦͯ͘͠ΕΔ ʢิʣ ɹɹɹ1ZUIPOɿΠςϨʔλΛฦ͢ ɹɹɹ1ZUIPOɿϦετΛฦ͢
pMUFS ݅ʹҰக͢ΔཁૉͷΈΛநग़͢Δ ྫ #ۮΛฦ͢ def is_even(val): if val % 2
== 0: return val print(list(filter(is_even, range(1,10)))) >>>[2, 4, 6, 8] #LambdaΛͬͯॻ͘ͱ print(list(filter(lambda x: x % 2 == 0, range(1, 10)))) >>>[2, 4, 6, 8]
NBQ શͯͷཁૉʹॲཧΛߦͬͨ݁ՌΛฦ͢ ྫ #ഒฦͩ͠ʂ def double(val): return val * 2
print(list(map(double, range(1,10)))) >>>[2, 4, 6, 8, 10, 12, 14, 16, 18] #LambdaΛͬͯॻ͘ͱ print(list(map(lambda x: x * 2, range(1, 10)))) >>>[2, 4, 6, 8]
SFEVDF શͯͷཁૉΛ·ͱΊΔ ʢ1ZUIPȌͰGVODUPPMTͷΠϯϙʔτ͕ඞཁʣ ྫ import functools #ͻͨ͢Βͯ͠ฦ͢ def add(val1, val2):
return val1 + val2 print(functools.reduce(add, range(1,10))) >>>45 #LambdaΛͬͯॻ͘ͱ print(functools.reduce(lambda x, y:x + y, range(1,10))) >>>45
ߴ֊ؔ ߴ֊ؔؔΛҾʹͨ͠Γɺ͋Δ͍ؔΛΓͱ͢ΔͷͰ͋Γɺ ҾΓͷؔ·ͨߴ֊ؔͱͳΓಘΔɻ 8JLJ1FEJBʹΑΔͱ
ߴ֊ؔ ҾʹؔΛ࣋ͭؔ Γ͕ؔʹͳΔؔ ΄΄͏ɾɾɾʁ ߴ֊ؔؔΛҾʹͨ͠Γɺ͋Δ͍ؔΛΓͱ͢ΔͷͰ͋Γɺ ҾΓͷؔ·ͨߴ֊ؔͱͳΓಘΔɻ 8JLJ1FEJBʹΑΔͱ
ͨͱ͑͜Μͳߴ֊ؔ PVUFS ͷҾʹIFMMP Λ͢ PVUFS ͷதͰJOOFS Λฦͯ͠Δ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func):
def inner(): print("before execute") func() print("after execute") return inner() #͜ΜʹͪPyFukuoka def hello(): print ("Hello PyFukuoka!") #࣮ߦ outer(hello) >>> before execute >>> Hello PyFukuoka! >>> after execute
࣮ߦ͞Ε͍ͯΔؔ ࣮ߦ͍ͯ͠ΔͷPVUFS ผ్IFMMP Λݺͼग़͢͜ͱՄೳ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): def inner():
print("before execute") func() print("after execute") return inner() #͜ΜʹͪPyFukuoka def hello(): print ("Hello PyFukuoka!") #࣮ߦ outer(hello) >>> before execute >>> Hello PyFukuoka! >>> after execute
ݩͷؔΛՃ͍͍ͯͪͪ͠ݺͼग़͢ͷʁ ʮ͜ͷؔΛݺͿͱ͖ઈରʹηοτͰݺͿॲཧʯ ͬͯ͋Γ·ͤΜʁ ͨͱ͑ɺ ɾϩάΠϯఆඞͣΒͳ͍ͱ͍͚ͳ͍ ɾ*O0VUͰϩάग़ྗ͕ඞཁʢࣗྗ4UBDL5SBDFʣ ͳͲͳͲɾɾ ͍͍ͪͪͯ͠ݺͼग़͢ͷ໘Ͱ͢ΑͶʁ
ͦ͜ͰσίϨʔλʔ ؆୯ʹ͍͏ͱɺߴ֊ؔͷϥούʔ ؔఆٛ࣌ʹʮˏϚʔΫʯΛ͚ͭΔ͜ͱͰɺ ఆٛ͞ΕͨؔΛ࣮ߦ͢Δͱ͖ʹࣗಈతʹߴ֊ؔ Λݺͼग़͢͜ͱ͕Ͱ͖Δ
ͨͱ͑ ઌ΄Ͳͷίʔυ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): def inner(): print("before execute") func()
print("after execute") return inner() #͜ΜʹͪPyFukuoka def hello(): print ("Hello PyFukuoka!") #࣮ߦ outer(hello) >>> before execute >>> Hello PyFukuoka! >>> after execute
IFMMP Λϥοϐϯά σίϨʔλʔΛ͏ͱ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): def inner(): print("before execute")
func() print("after execute") return inner() #͜ΜʹͪPyFukuoka @outer def hello(): print ("Hello PyFukuoka!") #࣮ߦ hello() >>> before execute >>> Hello PyFukuoka! >>> after execute IFMMP Λݺͼग़ͯ݁͠ՌมΘΒͣ
IFMMP Λϥοϐϯά σίϨʔλʔΛ͏ͱ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): def inner(): print("before execute")
func() print("after execute") return inner() #͜ΜʹͪPyFukuoka @outer def hello(): print ("Hello PyFukuoka!") #࣮ߦ hello() >>> before execute >>> Hello PyFukuoka! >>> after execute ͬͯΔ͜ͱ EFGIFMMP QSJOU lIFMMP1Z'VLVPLBz IFMMPPVUFS IFMMP IFMMP ͱಉ͜͡ͱ
࣮ߦ͞Ε͍ͯΔ໊લʹҙ ͜ͷ߹ɺPVUFSσίϨʔλʔΛద༻ͨ࣌͠Ͱ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): def inner(): print("before execute") func()
print("after execute") return inner() #͜ΜʹͪPyFukuoka @outer def hello(): print ("Hello PyFukuoka!") #࣮ߦ͞Ε͍ͯΔ໊ؔ print(hello.__name__) >>> inner ࣮ࡍʹ࣮ߦ͞ΕΔ͕ؔೖΕସΘΔ
ͱͷؔใΛอ͓࣋ͯͨ͘͠Ίʹ GVODUPSTXSBQTσίϨʔλΛ͏
GVODUPPMTXSBQTΛσίϨʔτ͢Δ ݩʑͷؔใΛͦͷ··දࣔ͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳͬͨʂ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ def outer(func): @functools.wraps(func) def inner(): print("before execute")
func() print("after execute") return inner() #͜ΜʹͪPyFukuoka @outer def hello(): print ("Hello PyFukuoka!") #࣮ߦ͞Ε͍ͯΔ໊ؔ print(hello.__name__) >>> hello
؆୯ʹ·ͱΊΔͱ ɾߴ֊ؔҾΓʹ͕ؔ͋Δؔ ɾσίϨʔλʔΛ͏ͱߴ֊ؔͷهड़͕؆୯ʹͳΔ ɾݩؔͷใΛอ࣋͢ΔʹGVODUPPMTXSBQTΛ͏
͓·͚ ࣮ߦݩͷؔʹҾ͕͋Δ߹ import functools def outer(func): @functools.wraps(func) def inner(*args, **kwargs):
print("before execute") func(*args, **kwargs) print("after execute") return inner @outer() def hello(*args, **kwargs): print(args[0]) print(kwargs["kwargs1"]) #࣮ߦ hello("hello", kwargs1 = "PyFukuoka") >>> before execute >>> hello >>> PyFukuoka >>> after execute
͓·͚ σίϨʔλʹ࣮ߦݩͷؔʹҾ͕͋Δ߹ import functools def outer(*dec_args, **dec_kwargs): def middle(func): @functools.wraps(func)
def inner(*args, **kwargs): print(dec_args[0]) print("before execute") func(*args, **kwargs) print("after execute") print(dec_kwargs["dec_kwargs1"]) return inner return middle #͜ΜʹͪPyFukuoka @outer("decorator args1", dec_kwargs1 = "decorator kwargs1") def hello(*args, **kwargs): print(args[0]) print(kwargs["kwargs1"]) #࣮ߦ hello("hello", kwargs1 = "PyFukuoka") >>> decorator args1 >>> before execute >>> hello >>> PyFukuoka >>> after execute >>> decorator kwargs1
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠