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
5
自分の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
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
170
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
140
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
Can We Measure Developer Productivity?
ewolff
1
150
DynamoDB でスロットリングが発生したとき_大盛りver/when_throttling_occurs_in_dynamodb_long
emiki
1
430
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Agile that works and the tools we love
rasmusluckow
327
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
A Philosophy of Restraint
colly
203
16k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Bash Introduction
62gerente
608
210k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Designing for Performance
lara
604
68k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠