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

Tasting "Python Distilled"

HayaoSuzuki
November 30, 2023

Tasting "Python Distilled"

HayaoSuzuki

November 30, 2023
Tweet

More Decks by HayaoSuzuki

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ ͓લ୭Α Name Hayao Suzukiʢླ໦ɹॣʣ ///////// Twitter X @CardinalXaro Work

    Software Developer @ BeProud Inc. › גࣜձࣾϏʔϓϥ΢υ › IT ษڧձࢧԉϓϥοτϑΥʔϜ › Python ಠֶϓϥοτϑΥʔϜ › γεςϜ։ൃυΩϡϝϯταʔϏε 2 / 60
  2. ࣗݾ঺հ ൃදͨ͠τʔΫʢൈਮʣ › SymPy ʹΑΔ਺ࣜॲཧ (PyCon JP 2018) › ΠϯϝϞϦʔετϦʔϜ׆༻ज़

    (PyCon JP 2020) › ૊ΈࠐΈؔ਺ pow ͷ஌ΒΕ͟ΔਐԽ (PyCon JP 2021) › Let’s implement useless Python objects(PyCon APAC 2023) https://xaro.hatenablog.jp/ ʹҰཡ͕͋Γ·͢ɻ 3 / 60
  3. ࣗݾ঺հ ຋༁ͨ͠ຊ › Python Distilled(O’Reilly Japan) ຊ೔ͷओ໾ ؂༁ͨ͠ຊ › ೖ໳

    Python 3 ୈ 2 ൛ (O’Reilly Japan) › ϩόετ Python(O’Reilly Japan) 4 / 60
  4. Python Distilled ݪஶɿPython Distilled ஶऀ David M. Beazley ग़൛೥ 2021

    ೥ 9 ݄ ग़൛ࣾ Addison-Wesley(Pearson) ๜༁ɿPython Distilled ϓϩάϥϛϯάݴޠ Python ͷΤοηϯε ༁ऀ ླ໦ ॣ ग़൛೥ 2023 ೥ 10 ݄ ग़൛ࣾ ΦϥΠϦʔɾδϟύϯ Pearson ͱͷܖ໿্ɺ๜༁ͷදࢴ͸ಈ෺Ͱ͸ͳ͍ɻ 7 / 60
  5. ຋༁ͷྲྀΕ ຋༁ग़൛·Ͱͷي੻ › 2022 ೥ 4 ݄ ڵຯຊҐͰݪஶిࢠ൛Λߪೖ › 2022

    ೥ 5 ݄ ΦϥΠϦʔͷฤूऀʹݪஶΛ঺հ͢ΔʢࡶஊϨϕϧʣ › 2022 ೥ 6 ݄ ຋༁൛ݖऔಘʹ޲͚ͯಈ͖ग़͢ › 2022 ೥ 7 ݄ ຋༁൛ݖऔಘɺ຋༁ͷଧ਍ɺ຋༁ʹ௅ઓ͠Α͏ͱܾҙ › 2022 ೥ 9 ݄ ຋༁Λ։࢝͢Δʢϩόετ Python ͷ؂༁ͱฒߦʣ › 2023 ೥ 4 ݄ Ұ௨Γ຋༁͕׬ྃɺਪᏏͷ೔ʑ › 2023 ೥ 9 ݄ ຋༁࡞ۀ׬ྃ 8 / 60
  6. Python Distilled ͬͯͲΜͳຊʁ ݪஶऀʮ͸͡ΊʹʯΑΓ ͜ͷʰPython Distilledʱ͸ Python ʹΑΔϓϩάϥϛϯάʹ͍ͭͯͷॻ੶Ͱ͢ɻ Python ͰՄೳͳ͜ͱ΍ɺ͋Δ͍͸ߦΘΕͨ͜ͱΛ͢΂ͯจॻԽ͠Α͏ͱ͍͏Θ͚Ͱ

    ͸͋Γ·ͤΜɻຊॻͷ໨త͸ɺݱ୅తͰ͋Γݫબɺͭ·Γৠཹʢdistilledʣ͞Εͨϓ ϩάϥϛϯάݴޠ Python ͷ֩৺Λ঺հ͢Δ͜ͱͰ͢ɻ ʢதུʣ͔͠͠ɺͦΕ͸·ͨɺ ιϑτ΢ΣΞϥΠϒϥϦΛॻ͖ɺPython ͷԿͨΔ͔Λ஌ΓɺԿ͕࠷΋໾ʹཱ͔ͭΛ ݟग़ͨ݁͠ՌͰ΋͋ΔͷͰ͢ɻ 9 / 60
  7. ॻධ ///////// Twitter X Ͱݟ͔͚ͨॻධ No human should be allowed

    to write Python code before reading it. ौ઒͞ΜʹΑΔॻධ ண࣮ʹ Python ΛࣗΒͷ݂೑ʹ͍͖͍ͯͨ͠ਓ޲͚ͷຊͰ͢ɻ 11 / 60
  8. ୈ 1 ໰ ໰୊ɿγϯάϧτϯͷൺֱ None ΍ TrueɺFalse ͸γϯάϧτϯͰ͋Γɺγϯάϧτϯ͸ is จͰൺֱ͠·͢ɻ

    ͜ͷ஫ҙࣄ߲͸υΩϡϝϯτͷͲ͜ʹॻ͔Ε͍ͯΔͰ͠ΐ͏͔ʁ ղ౴ɿ2 Օॴ › ݴޠϦϑΝϨϯεʢ஫ҙשىʣ › PEP 8ʢ؆୯ͳཧ༝΋ʣ ࣮࣭తʹ͸ PEP 8 ͚ͩͱ΋ݴ͑Δɻ 17 / 60
  9. ୈ 3 ໰ ໰୊ɿwith จ Python 2.5 ͔Β with จ͕ಋೖ͞Ε·ͨ͠ɻwith

    จͷ࢖͍ํ͸Ͳ͜ʹॻ͔Ε͍ͯ ΔͰ͠ΐ͏͔ʁ 20 / 60
  10. ୈ 3 ໰ ໰୊ɿwith จ Python 2.5 ͔Β with จ͕ಋೖ͞Ε·ͨ͠ɻwith

    จͷ࢖͍ํ͸Ͳ͜ʹॻ͔Ε͍ͯ ΔͰ͠ΐ͏͔ʁ ղ౴ɿ3 Օॴ › νϡʔτϦΞϧʢଘࡏΛࣔࠦ͢Δ͚ͩʣ › ݴޠϦϑΝϨϯεʢwith จͷߏจͱίϯςΩετϚωʔδϟʹ͍ͭͯʣ › PEP 343ʢwith ͷಋೖܦҢ΍എܠʹ͍ͭͯʣ 21 / 60
  11. ୈ 5 ໰ ໰୊ɿfrom module import * from module import

    *͕Մೳͳͷ͸ϞδϡʔϧϨϕϧͷΠϯϙʔτͰɺΫϥε΍ؔ ਺಺෦Ͱ͸Ͱ͖·ͤΜɻ͜ͷࣄ࣮ʹ͍ͭͯॻ͔Ε͍ͯΔͷ͸Ͳ͜Ͱ͠ΐ͏͔ʁ 24 / 60
  12. ୈ 5 ໰ ໰୊ɿfrom module import * from module import

    *͕Մೳͳͷ͸ϞδϡʔϧϨϕϧͷΠϯϙʔτͰɺΫϥε΍ؔ ਺಺෦Ͱ͸Ͱ͖·ͤΜɻ͜ͷࣄ࣮ʹ͍ͭͯॻ͔Ε͍ͯΔͷ͸Ͳ͜Ͱ͠ΐ͏͔ʁ ղ౴ɿ1 Օॴ › ݴޠϦϑΝϨϯε ͨͩ͠ɺfrom module import *͸࢖͏ͳͱ஫ҙשى͞Ε͍ͯΔ 25 / 60
  13. Python ͷֶͼํ Python ͸ެࣜυΩϡϝϯτ͕ॆ࣮ › େମެࣜυΩϡϝϯτ΍ PEP ʹॻ͔Ε͍ͯΔ › νϡʔτϦΞϧͱඪ४ϥΠϒϥϦ͚ͩͰԿͱ͔ͳΔ

    ެࣜυΩϡϝϯτ͸๲େ͗͢Δ › ಥͬࠐΜͩ಺༰ͩͱݴޠϦϑΝϨϯε΍ PEP Λ୳Δ͜ͱʹͳΔ › ݴޠϦϑΝϨϯε͸ʮ͚ͦͬͳ͍ॻ͖ํʯ ɺಡΈ෺తʹಡΊͳ͍ɻ 26 / 60
  14. Python ͷֶͼํ ॻ੶͔ΒֶͿɿೖ໳ॻ › جຊతʹ͸ॳ৺ऀ޲͖ɺಥͬࠐΜͩ಺༰ʹ͸৮Ε͍ͯͳ͍ › චऀͷ޻෉ͱͯ͠औࣺબ୒͕ߦΘΕ͍ͯΔ ೖ໳ॻͷ۩ମྫ › ʰೖ໳

    Python 3 ୈ 2 ൛ʱ ʢ800 ϖʔδʣ ɺೖ໳෦෼͸ 270 ϖʔδ › ʰPython νϡʔτϦΞϧ ୈ 4 ൛ʱ ʢ264 ϖʔδʣ ɺఈຊ͸ެࣜυΩϡϝϯτ 27 / 60
  15. Python ͷֶͼํ ॻ੶͔ΒֶͿɿ ʢൺֱతʣߴ͍Ϩϕϧͷຊ › Python ݴޠΛ໢ཏ͠Α͏ͱ͍ͯ͠Δ › ඞવతʹ෼ް͘ͳΓɺಡΈ௨͢ͷ͕େม ߴ͍Ϩϕϧͷ۩ମྫ

    › ʰॳΊͯͷ Python ୈ 3 ൛ʱ ʢ808 ϖʔδʣ ɺݪॻୈ 5 ൛͸ 1648 ϖʔδ › ʰFluent Pythonʱ ʢ832 ϖʔδʣ ɺݪॻୈ 2 ൛͸ 983 ϖʔδ 28 / 60
  16. Python ͷֶͼํ Πϯλʔωοτɺ·ͨ͸ݕࡧͰ୳͢ › ۄੴࠞ߹ › ݁ہ͸ެࣜυΩϡϝϯτʹམͪண͘ › ௐ΂͍ͨ͜ͱ͕Θ͔͍ͬͯͳ͍ͱ࢖͑ͳ͍ ඼࣭͕ߴ͍

    Web ࢿྉ › ʰPython Boot Camp Textʱॳ৺ऀ޲͚νϡʔτϦΞϧΠϕϯτͷࢿྉ › ʰPython TutorʱPython ͷಈ͖Λࢹ֮తʹ֬ೝͰ͖ΔαΠτ 29 / 60
  17. Python ͷֶͼํ ࠷ۙͷྲྀߦΓɿGPTs ʹฉ͍ͯΈΔ › Զͨͪͷ ChatGPT ઌੜ › ௐ΂͍ͨ͜ͱ͕Θ͔͍ͬͯͳ͍ͱ࢖͑ͳ͍

    › ͖ͨͨ୆ͱͯ͠࠷ద › ͋Δఔ౓ Python ΛΘ͔͍ͬͯͳ͍ͱ࢖͍͜ͳͤͳ͍ʢࢲݟʣ ΤϨϛϠ 14:14ʢ੟ॻڠձڞಉ༁ΑΓʣ ओ͸ࢲʹݴΘΕͨɻ ʮ༬ݴऀͨͪ͸ɺࢲͷ໊ʹΑِͬͯΓͷ༬ݴΛ͍ͯ͠Δɻࢲ͸൴Β ΛݣΘͨ͜͠ͱ΋ͳ͘ɺ൴Βʹ໋ͨ͜͡ͱ΋ͳ͘ɺ൴Βʹޠͬͨ͜ͱ΋ͳ͍ɻ൴Β͸ِ Γͷݬͱۭ͍͠઎͍ͱࣗ෼ͷ৺ͷ͖ٗΛ͋ͳ͕ͨͨʹ༬ݴ͍ͯ͠Δͷͩɻ 30 / 60
  18. Python ͷֶͼํ Python Distilled › Python ݴޠͦͷ΋ͷʹಛԽͨ͠ຊ › ݴޠϦϑΝϨϯεʹॻ͍ͯ͋Δ͜ͱ͕ 336

    ϖʔδʹ·ͱ·͍ͬͯΔ Python Distilled ʹॻ͍͍ͯͳ͍͜ͱ › ܕώϯτपΓʢ ʰϩόετ Pythonʱಡ΋͏ʣ › ඇಉظॲཧ › 3rd ύʔςΟϥΠϒϥϦɺΤίγεςϜपΓ 31 / 60
  19. FAQ ຊ౰ʹΑ͋͘Δ࣭໰ ʰPython Distilledʱͱʰೖ໳ Python 3 ୈ 2 ൛ʱͷҧ͍͸ʁ Python

    Distilled ʹॻ͍͍ͯͳ͍͜ͱ › ʰೖ໳ Python 3 ୈ 2 ൛ʱ͸ΤίγεςϜ΋͔ͬ͠Γ৮Ε͍ͯΔ › ʰPython Distilledʱ͸ݴޠίΞʹಛԽͨ͠ຊ 32 / 60
  20. 1 ষ Python ͷجૅ 1 ষ Python ͷجૅ › ม਺΍σʔλܕɺࣜɺ੍ޚߏ଄ɺؔ਺ɺΫϥεɺೖग़ྗʹ͍ͭͯͷ֓આ

    › ݪॻ͸ Python 3.8 Ҏ߱Λ૝ఆɺ຋༁Ͱ͸ 3.11 ·ͰରԠͰ͖ΔΑ͏ʹͨ͠ › Θ͔Δਓ͸ඈ͹ͯ͠΋େৎ෉ 34 / 60
  21. 2 ষ ԋࢉࢠɺࣜɺσʔλૢ࡞ ࣮ߦྫ >>> f(4) [4] >>> a =

    [] >>> f(3, a) [3] >>> a # ߋ৽͞Εͳ͍ʂ [] 38 / 60
  22. 3 ষ ϓϩάϥϜͷߏ଄ͱ੍ޚߏ଄ ྫɿྫ֎ͷ࿈࠯ try: x = int("N/A") except Exception

    as e: raise ApplicationError("It failed") from e ྫɿ૝ఆ֎ͷ࿈࠯ try: x = int("N/A") except Exception as e: print("It failed:", err) # NameError 41 / 60
  23. 4 ষ ΦϒδΣΫτɺܕɺϓϩτίϧ ྫɿࢀরΧ΢ϯτ >>> a = 37 # ஋

    37 Λ࣋ͭΦϒδΣΫτΛ࡞੒͢Δ >>> b = a # ΦϒδΣΫτͷࢀরΧ΢ϯτ૿Ճ >>> c = [] >>> c.append(b) # ΦϒδΣΫτͷࢀরΧ΢ϯτ૿Ճ 44 / 60
  24. 4 ষ ΦϒδΣΫτɺܕɺϓϩτίϧ ྫɿࢀরͱίϐʔ >>> a = [1, 2, 3,

    4] >>> b = a # b ͸ a ͷࢀর >>> b is a True >>> b[2] = -100 # b ͷཁૉΛมߋ͢Δ >>> a # a ͷཁૉ΋มߋ͞ΕΔ [1, 2, -100, 4] 45 / 60
  25. 4 ষ ΦϒδΣΫτɺܕɺϓϩτίϧ ྫɿ੔਺ܕͱුಈখ਺఺਺ܕ >>> a = 42 >>> b

    = 3.7 >>> a.__add__(b) NotImplemented >>> b.__radd__(a) 45.7 46 / 60
  26. 5 ষ ؔ਺ ྫɿಈతͳؔ਺ੜ੒ def make_init(*names): params = ", ".join(names)

    code = f"def __init__(self, {params}):\n" for name in names: code += f" self.{name} = {name}\n" d = {} exec(code, d) return d["__init__"] NamedTuple ΍@dataclass Ͱ׆༻͞Ε͍ͯΔςΫχοΫ 48 / 60
  27. 6 ষ δΣωϨʔλ ྫɿδΣωϨʔλͷҕৡ def flatten(items): for i in items:

    if isinstance(i, list): yield from flatten(i) else: yield i a = [1, 2, [3, [4, 5], 6, 7], 8] for x in flatten(a): print(x, end=" ") 50 / 60
  28. 6 ষ δΣωϨʔλ ྫɿ֦ுδΣωϨʔλ def receiver(): print("Ready to receive") while

    True: n = yield print("Got", n) >>> r = receiver() >>> r.send(None) Ready to receive >>> r.send("Hello") Got Hello >>> r.close() 51 / 60
  29. 8 ষ Ϟδϡʔϧͱύοέʔδ import module ͱ from module import func

    ͷҧ͍ › import module ͸৽ͨʹ໊લۭؒΛੜ੒͢Δ › from module import func ͸࣮ߦ͞Ε໊ͨલۭؒʹ func Λ௥Ճ͢Δ from module import func ͷํ͕଎͍ʁ › ؾͷ͍ͤͰ͢ › Python ͕ཪଆͰ import module Λ͢ΔͷͰɺؔ܎͋Γ·ͤΜ 55 / 60
  30. 9 ষ ೖྗͱग़ྗ ྫɿopen() ؔ਺ͷཪଆ import io raw = io.FileIO("filename.txt",

    "r") # ੜόΠφϦϞʔυ buffered = io.BufferedReader(raw) # όοϑΝ෇͖όΠφϦಡΈࠐΈ file = io.TextIOWrapper(buffered, encoding="utf-8") # ςΩετϞʔυ 57 / 60
  31. ·ͱΊ ͬͦ͘͞ߪೖ͠Α͏ › https://www.oreilly.co.jp/books/9784814400461/ › https://www.ohmsha.co.jp/book/9784814400461/ ΦϥΠϦʔֶशϓϥοτϑΥʔϜͱ͸ › https://www.oreilly.co.jp/online-learning/ ›

    6 ສ࡭Ҏ্ͷॻ੶ͱ 3 ສ࣌ؒҎ্ͷಈըʢ೔ຊޠ΋͋Δʂʣ › ۀքΤΩεύʔτʹΑΔϥΠϒΠϕϯτ › ΠϯλϥΫςΟϒͳγφϦΦͱαϯυϘοΫεΛ࢖࣮ͬͨફతͳֶश › ެࣜೝఆࢼݧରࡦࢿྉ › ʰPython Distilledʱ΋ΦϥΠϦʔֶशϓϥοτϑΥʔϜͰಡΈ์୊ʢ͍͢͝ʣ 60 / 60