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
200
自分の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
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
410
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
120
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
170
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
180
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
270
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
なぜCodeceptJSを選んだか
goataka
0
160
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
540
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
13
3.7k
2024年にチャレンジしたことを振り返るぞ
mitchan
0
140
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
200
Featured
See All Featured
Building an army of robots
kneath
302
44k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Why Our Code Smells
bkeepers
PRO
335
57k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
Adopting Sorbet at Scale
ufuk
73
9.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠