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
500
私の知らないPythonの世界
pyfukuoka #3
at 2017/12/20
in Fukuoka City
massyuu
December 20, 2017
Tweet
Share
More Decks by massyuu
See All by massyuu
自分のTwitter利用を振り返ってみる
massyuu
0
82
Vue×Nativeの開発を簡単にしたかったのです
massyuu
0
380
Authから始めるFirebaseの第一歩
massyuu
1
1.3k
RealWorldHTTPを読んでみた
massyuu
1
530
Pythonのアプリ開発にElectronという選択肢
massyuu
1
2.3k
Other Decks in Technology
See All in Technology
チームを主語にしてみる / Making "Team" the Subject
ar_tama
4
300
大規模データ基盤チームのオンプレTiDB運用への挑戦 / dpu-tidb
cyberagentdevelopers
PRO
1
110
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
730
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
620
急成長中のWINTICKETにおける品質と開発スピードと向き合ったQA戦略と今後の展望 / winticket-autify
cyberagentdevelopers
PRO
1
160
物価高なラスベガスでの過ごし方
zakky
0
360
新R25、乃木坂46 Mobileなどのファンビジネスを支えるマルチテナンシーなプラットフォームの全体像 / cam-multi-cloud
cyberagentdevelopers
PRO
1
130
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
一休.comレストランにおけるRustの活用
kymmt90
3
580
Amazon_CloudWatch_ログ異常検出_導入ガイド
tsujiba
4
1.5k
「 SharePoint 難しい」ってよく聞くけど、そんなに言うなら8歳の息子に試してもらった
taichinakamura
1
600
AIを駆使したゲーム開発戦略: 新設AI組織の取り組み / sge-ai-strategy
cyberagentdevelopers
PRO
1
130
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
How STYLIGHT went responsive
nonsquared
95
5.2k
KATA
mclloyd
29
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
We Have a Design System, Now What?
morganepeng
50
7.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
328
21k
4 Signs Your Business is Dying
shpigford
180
21k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
For a Future-Friendly Web
brad_frost
175
9.4k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠