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
ガチ勢によるPipeCD運用大全〜滑らかなCI/CDを添えて〜 / ai-pipecd-encyclopedia
cyberagentdevelopers
PRO
3
200
ネット広告に未来はあるか?「3rd Party Cookie廃止とPrivacy Sandboxの効果検証の裏側」 / third-party-cookie-privacy
cyberagentdevelopers
PRO
1
130
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
【技術書典17】OpenFOAM(自宅で極める流体解析)2次元円柱まわりの流れ
kamakiri1225
0
210
大規模データ基盤チームのオンプレTiDB運用への挑戦 / dpu-tidb
cyberagentdevelopers
PRO
1
110
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
Fargateを使った研修の話
takesection
0
110
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.6k
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
よくわからんサービスについての問い合わせが来たときの強い味方 Amazon Q について
kazzpapa3
0
220
プロダクトチームへのSystem Risk Records導入・運用事例の紹介/Introduction and Case Studies on Implementing and Operating System Risk Records for Product Teams
taddy_919
1
170
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
Building Better People: How to give real-time feedback that sticks.
wjessup
363
19k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Become a Pro
speakerdeck
PRO
24
5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
Six Lessons from altMBA
skipperchong
26
3.5k
Statistics for Hackers
jakevdp
796
220k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Embracing the Ebb and Flow
colly
84
4.4k
Git: the NoSQL Database
bkeepers
PRO
425
64k
Building Your Own Lightsaber
phodgson
102
6k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠