Upgrade to Pro — share decks privately, control downloads, hide ads and more …

私の知らないPythonの世界

44749d6ac5ca44e5325622593877c420?s=47 massyuu
December 20, 2017

 私の知らないPythonの世界

pyfukuoka #3
at 2017/12/20
in Fukuoka City

44749d6ac5ca44e5325622593877c420?s=128

massyuu

December 20, 2017
Tweet

Transcript

  1. ࢲͷ஌Βͳ͍ 1ZUIPOͷੈք !NBTTZVV 1Z'VLVPLB  ʙσίϨʔλʔΛͦ͑ͯʙ

  2. ࣗݾ঺հ !NBTTZVV 1ZUIPO͸झຯͰ࢖ͬͯ·͢ ͔Β͋͛ΛࡇΓਸΊΔΤϯδχϟʔ ϑϩϯτ͔ΒόοΫΤϯυ·Ͱ޿͘ઙ͘ 4PTJBM UXJUUFSNBTT@TTO DPOOQBTTɿNBTTZVV

  3. ࠓ೔ͷ͓࿩͸ 1ZUIPOΛ࢖͍࢝Ίͯ ʮ΄΄͏ʯ ͱࢥͬͨ͜ͱΛ 5JQTతʹ·ͱΊ·ͨ͠

  4. ࠓ೔ͷ͓࿩͸ 1ZUIPOΛ࢖͍࢝Ίͯ ʮ΄΄͏ʯ ͱࢥͬͨ͜ͱΛ 5JQTతʹ·ͱΊ·ͨ͠

  5. ࠓ೔ͷ͓࿩͸ ߴ֊ؔ਺ͬͯͳΜ͡Ό͍ʁ σίϨʔλʔͬͯͳΜ͡Ό͍ʁ ͱ͍͏͓࿩Ͱ͢

  6. ͓඼ॻ͖ ɾpMUFSɺNBQɺSFEVDF ɾߴ֊ؔ਺ ɾσίϨʔλʔ ɾδΣωϨʔλʔ ɾ಺แදهͱ͔ͱ͔

  7. ஫ҙ ࠓճ͸1ZUIPOͰͷ͓࿩Ͱ͢ ʢ1ZUIPOͱ͸ॻ͖ํ͕ҟͳΔ৔߹͕͋Γ·͢ʣ

  8. pMUFS NBQ ΠςϨʔλΛ౉͢ͱɺͦΕͧΕରԠͨ͠ॲཧΛߦͬͯ ΠςϨʔλΛฦͯ͘͠ΕΔ ʢิ଍ʣ ɹɹɹ1ZUIPOɿΠςϨʔλΛฦ͢ ɹɹɹ1ZUIPOɿϦετΛฦ͢

  9. 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]
  10. 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]
  11. 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
  12. ߴ֊ؔ਺ ߴ֊ؔ਺͸ؔ਺ΛҾ਺ʹͨ͠Γɺ͋Δ͍͸ؔ਺Λ໭Γ஋ͱ͢Δ΋ͷͰ͋Γɺ Ҿ਺΍໭Γ஋ͷؔ਺΋·ͨߴ֊ؔ਺ͱͳΓಘΔɻ 8JLJ1FEJBʹΑΔͱ

  13. ߴ֊ؔ਺ Ҿ਺ʹؔ਺Λ࣋ͭؔ਺ ໭Γ஋͕ؔ਺ʹͳΔؔ਺ ΄΄͏ɾɾɾʁ ߴ֊ؔ਺͸ؔ਺ΛҾ਺ʹͨ͠Γɺ͋Δ͍͸ؔ਺Λ໭Γ஋ͱ͢Δ΋ͷͰ͋Γɺ Ҿ਺΍໭Γ஋ͷؔ਺΋·ͨߴ֊ؔ਺ͱͳΓಘΔɻ 8JLJ1FEJBʹΑΔͱ

  14. ͨͱ͑͹͜Μͳߴ֊ؔ਺ 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
  15. ࣮ߦ͞Ε͍ͯΔؔ਺ ࣮ߦ͍ͯ͠Δͷ͸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
  16. ݩͷؔ਺Λ௥Ճ͍͍ͯͪͪ͠ݺͼग़͢ͷʁ ʮ͜ͷؔ਺ΛݺͿͱ͖͸ઈରʹηοτͰݺͿॲཧʯ ͬͯ͋Γ·ͤΜʁ ͨͱ͑͹ɺ ɾϩάΠϯ൑ఆ͸ඞͣ΍Βͳ͍ͱ͍͚ͳ͍ ɾ*O0VUͰϩάग़ྗ͕ඞཁʢࣗྗ4UBDL5SBDFʣ ͳͲͳͲɾɾ ͍͍ͪͪ౉ͯ͠ݺͼग़͢ͷ໘౗Ͱ͢ΑͶʁ

  17. ͦ͜ͰσίϨʔλʔ ؆୯ʹ͍͏ͱɺߴ֊ؔ਺ͷϥούʔ ؔ਺ఆٛ࣌ʹʮˏϚʔΫʯΛ͚ͭΔ͜ͱͰɺ ఆٛ͞Εͨؔ਺Λ࣮ߦ͢Δͱ͖ʹࣗಈతʹߴ֊ؔ਺ Λݺͼग़͢͜ͱ͕Ͱ͖Δ

  18. ͨͱ͑͹ ઌ΄Ͳͷίʔυ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ 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
  19. 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 Λݺͼग़ͯ͠΋݁Ռ͸มΘΒͣ
  20. 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  ͱಉ͜͡ͱ
  21. ࣮ߦ͞Ε͍ͯΔ໊લʹ஫ҙ ͜ͷ৔߹ɺ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 ࣮ࡍʹ࣮ߦ͞ΕΔؔ਺͕ೖΕସΘΔ
  22. ΋ͱͷؔ਺৘ใΛอ͓࣋ͯͨ͘͠Ίʹ͸ GVODUPSTXSBQTσίϨʔλΛ࢖͏

  23. 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
  24. ؆୯ʹ·ͱΊΔͱ ɾߴ֊ؔ਺͸Ҿ਺΍໭Γ஋ʹؔ਺͕͋Δؔ਺ ɾσίϨʔλʔΛ࢖͏ͱߴ֊ؔ਺ͷهड़͕؆୯ʹͳΔ ɾݩؔ਺ͷ৘ใΛอ࣋͢Δʹ͸GVODUPPMTXSBQTΛ࢖͏

  25. ͓·͚ ࣮ߦݩͷؔ਺ʹҾ਺͕͋Δ৔߹ 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
  26. ͓·͚ σίϨʔλʹ΋࣮ߦݩͷؔ਺ʹ΋Ҿ਺͕͋Δ৔߹ 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
  27. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠