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

私の知らないPythonの世界

Avatar for massyuu massyuu
December 20, 2017

 私の知らないPythonの世界

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

Avatar for massyuu

massyuu

December 20, 2017
Tweet

More Decks by massyuu

Other Decks in Technology

Transcript

  1. 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]
  2. 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]
  3. 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
  4. ͨͱ͑͹͜Μͳߴ֊ؔ਺ 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
  5. ࣮ߦ͞Ε͍ͯΔؔ਺ ࣮ߦ͍ͯ͠Δͷ͸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
  6. ͨͱ͑͹ ઌ΄Ͳͷίʔυ #࣮ߦલޙʹςΩετΛࠩ͠ࠐΉΑʂ 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
  7. 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 Λݺͼग़ͯ͠΋݁Ռ͸มΘΒͣ
  8. 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  ͱಉ͜͡ͱ
  9. ࣮ߦ͞Ε͍ͯΔ໊લʹ஫ҙ ͜ͷ৔߹ɺ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 ࣮ࡍʹ࣮ߦ͞ΕΔؔ਺͕ೖΕସΘΔ
  10. 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
  11. ͓·͚ ࣮ߦݩͷؔ਺ʹҾ਺͕͋Δ৔߹ 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
  12. ͓·͚ σίϨʔλʹ΋࣮ߦݩͷؔ਺ʹ΋Ҿ਺͕͋Δ৔߹ 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