The Zen Of Python / 2012-07-28

The Zen Of Python / 2012-07-28

2012年7月28日に行われた、母校の学生・OBを対象にした勉強会のLTで使用したスライドです。後期のプログラミング言語の授業でPythonを扱うとのことなので、Pythonに興味を持ってもらいたいという思いで発表しました。Pythonという言語の存在すら知らない学生さんが対象だったので、技術的な話はほとんど含まれていません。

6fa3836d10d691125749472297cf516a?s=128

7pairs

July 28, 2012
Tweet

Transcript

  1. The Zen of Python Jun-ya HASEBA

  2. What is Python? • Guido van RossumʹΑͬͯ։ൃ͞ΕͨΦϒδΣΫ τࢦ޲εΫϦϓτݴޠɻ • ڭҭ༻Λҙਤͯ͠։ൃ͞ΕͨݴޠͰ͸ͳ͍͕ɺͦͷ

    ಛ௃͔Βڭҭʹ༻͍ΒΕΔ͜ͱ΋ଟ͍ɻ • ඪ४Ͱ๛෋ͳϥΠϒϥϦ͕༻ҙ͞Ε͓ͯΓɺϓϩά ϥϛϯάΛڧྗʹαϙʔτͯ͘͠ΕΔɻ
  3. The Zen of Python • Tim PetersʹΑͬͯॻ͔ΕͨɺPythonͷجຊతͳߟ ͑ํΛ·ͱΊͨશ19৚ͷࢦ਑ɻ • PythonΠϯλϓϦλ্Ͱ

    import this ͱೖྗ͢ Ε͹ɺͦͷ಺༰Λ͍ͭͰ΋֬ೝͰ͖Δɻ • ઈରͷϧʔϧͱ͍͏Θ͚Ͱ͸ͳ͍͕ɺϓϩάϥϛϯ άͷࡍʹ͸಄ͷย۱ʹஔ͍͓͖͍ͯͨɻ
  4. 1. Beautiful is better than ugly. • ʲ༁ʳ͖ͨͳ͍ͷΑΓ͖Ε͍ͳํ͕͍͍ɻ • ॻ͍ͨ͋ͱɺ୭ʹ΋ಡ·ΕΔ͜ͱͷͳ͍ϓϩάϥϜ

    ͸ଘࡏ͠ͳ͍ɻ • ΄͔ͷਓͷͨΊʹɺͦͯ͠ະདྷͷࣗ෼ͷͨΊʹ៉ྷ ͳϓϩάϥϜΛॻ͜͏ɻ
  5. 2. Explicit is better than implicit. • ʲ༁ʳͪ͝Όͪ͝Ό೉͍͠ͷΑΓɺനࠇ͸͖ͬΓ͠ ͯΔͷ͕͍͍ɻ •

    ϓϩάϥϜதͷม਺ɺؔ਺ɺΫϥεɺύοέʔδ… ͳͲʹ͸ɺҙຯ͕໌֬ʹΘ͔Δ໊લΛ͚ͭΑ͏ɻ • ΩϥΩϥม਺໊Λ͚ͭΔͷ͸΍ΊΑ͏ɻ
  6. 3. Simple is better than complex. • ʲ༁ʳΊΜͲ͏ͳͷΑΓ͔ΜͨΜͳํ͕͍͍ɻ • ؆୯ͳ͜ͱ͸؆୯ʹॻ͜͏ɻ

    • ֨޷Λ͚ͭͯΞΫϩόςΟοΫͳॻ͖ํΛ͢Δͷ͸ ΍ΊΑ͏ɻ
  7. 4. Complex is better than complicated. • ʲ༁ʳ͚Ͳɺ༁෼͔Μͳ͘ͳΔ͘Β͍ͳΒΊΜͲ͏ ͳํ͕·͠ɻ •

    ෳࡶͳ͜ͱΛ؆୯ʹॻ͜͏ͱͯ͠ɺٯʹҙຯ͕఻Θ ΓͮΒ͘ͳ͍ͬͯͳ͍ͩΖ͏͔ʁ • Θ͔ΓͮΒ͔ͬͨΓɺ΄͔ͷ෦෼ʹѱӨڹ͕ग़ͨΓ ͢ΔͷͰ͋Ε͹ɺผʹແཧΛ͢Δඞཁ͸ͳ͍ɻ
  8. 5. Flat is better than nested. • ʲ༁ʳωετͤ͞ͳ͍͍ͯ͘ͳΒ͠ͳ͍ํ͕͍͍ɻ • ωετ͕ਂ͘ͳͬͯ͠·͏৔߹ɺϓϩάϥϜͷߏ଄

    Λݟ௚͢΂͖͔΋͠Εͳ͍ɻ • ʮωετͬͯԿʁʯͱٙ໰ʹࢥͬͨਓ͸ɺޙظͷत ۀͰઌੜʹ࣭໰͢Ε͹͖ͬͱஸೡʹڭ͑ͯ΋Β͑Δ Αʂ
  9. 6. Sparse is better than dense. • ʲ༁ʳͨ͘͞Μ٧ΊࠐΜͩͷΑΓεΧεΧͳํ͕͍ ͍ɻ •

    ϓϩάϥϜʹ͸ద౓ʹۭߦΛೖΕΑ͏ɻ • ͨ͘͞ΜͷॲཧΛ1ߦʹ٧ΊࠐΉͷ͸΍ΊΑ͏ɻ • ͜ͷεϥΠυʹ΋Zenͷߟ͑ํΛऔΓࠐΈɺ͋͑ͯ εΧεΧʹ࡞ͬͯ͋Γ·͢ɻ
  10. 7. Readability counts. • ʲ༁ʳಡΈ΍͕͍ͨͤͭ͢͞ͳͷΑɻ • PythonͰ͸ϓϩάϥϜͷಡΈ΍͕͢͞ಛʹॏཁࢹ͞ ΕΔɻ • ΄͔ͷϝϦοτΛ௥ٻ͢Δ͋·ΓɺಡΈ΍͢͞Λଛ

    Ͷͯ͠·Θͳ͍Α͏ʹؾΛ͚ͭΑ͏ɻ
  11. 8. Special cases aren't special enough to break the rules.

    9. Although practicality beats purity. • ʲ༁ʳಛผͳ͜ͱ΋͋Δ͚ͲᎄഁΓͬͯ΄Ͳ͡Όͳ ͍ɻ࣮༻ੑΛٻΊͯ͘ͱɺͪΐͬͱ͸ͣΕͪΌ͏͜ ͱ΋͋Δ͚ͲͶɻ • ͦͷಛผѻ͍͕ຊ౰ʹඞཁͳ͜ͱ͔Ͳ͏͔ɺ΋͏Ұ ౓Α͘ߟ͑ͯΈΑ͏ɻ • ΋͠ຊ౰ʹඞཁͳͷͰ͋Ε͹ɺಛผѻ͍ࣗମ͸ผʹ ѱ͍͜ͱͰ͸ͳ͍ɻ
  12. 10. Errors should never pass silently. 11. Unless explicitly silenced.

    • ʲ༁ʳΘ͟ͱͦ͏ͯ͠ΔΜ͡Όͳ͍ݶΓɺΤϥʔΛ ͩ·ͬͯ௨͢Α͏ͳ͜ͱ͕͋ͬͪΌ͍͚·ͤΜɻ • ྫ֎ॲཧ͸ෆ঵ࣄΛӅͨ͢Ίͷ࢓૊ΈͰ͸ͳ͍ɻ • ʮྫ֎ॲཧͬͯԿʁʯͱٙ໰ʹࢥͬͨਓ͸ɺޙظͷ तۀͰઌੜʹ࣭໰͢Ε͹͖ͬͱஸೡʹڭ͑ͯ΋Β͑ ΔΑʂ
  13. 12. In the face of ambiguity, refuse the temptation to

    guess. • ʲ༁ʳ͍͋·͍ͳ͜ͱΛ͖ͯͱʔʹॲཧͪ͠Ό͍͚ ·ͤΜɻ • ࢥ͍ࠐΈ΍ܾΊ͚ͭͰॲཧΛͯ͠͸͍͚ͳ͍ɻ • ͋ͳͨͷϓϩάϥϜΛ࢖͏ਓ͸ɺ͋ͳ͕ͨࢥ͍΋Α Βͳ͔ͬͨΑ͏ͳૢ࡞ΛฏؾͰߦ͏ͱ͍͏͜ͱΛ؊ ʹ໏͓ͯ͜͡͏ɻ
  14. 13. There should be one - and preferably only one

    - obvious way to do it. 14. Although that way may not be obvious at first unless you're Dutch. • ʲ༁ʳؒҧ͑Α͏ͷͳ͍΍Γํ͕ͻͱ͚ͭͩ͋Δͷ ͕͍͍ͶɻΦϥϯμਓҎ֎ʹ͸ɺͪΐͬͱ෼͔Γʹ ͔ͬͨ͘Γͯ͠΋Ͷɻ • There’s Only One Way To Do It. • ͍͘Βಉ͡ॻ͖ํʹͳΔ͔Βͱ͍ͬͯɺ΄͔ͷਓͷ ՝୊ͷ౴͑Λؙࣸ͠ʹͯ͠ఏग़͢Δͷ͸΍ΊΑ͏ɻ
  15. 15. Now is better than never. 16. Although never is

    often better than *right* now. • ʲ༁ʳ΍Βͳ͍ΑΓ͸ࠓ΍Δ΂͖ɻ͚Ͳࠓʮ͙͢ʯ ΍ΔͳΒ΍Μͳ͍ํ͕͍͍͜ͱ΋͋Δɻ • ໘౗͔ͩΒͱ͍ͬͯޙճ͠ʹ͢Δͷ͸΍ΊΑ͏ɻͰ ΋ɺߦಈʹҠ͢ͷ͸Α͘ߟ͔͑ͯΒɻ • ৻ॏ͔ͭେ୾ʹɻ
  16. 17. If the implementation is hard to explain, its a

    bad idea. 18. If the implementation is easy to explain, it may be a good idea. • ʲ༁ʳ࡞Δ΋ͷΛ͏·͘આ໌Ͱ͖ͳ͍Α͏ͳΒͦΕ ͸Ϙπɻ͏·͘આ໌Ͱ͖ͨΒͨͿΜͦΕ͕άουɻ • ΄͔ͷਓʹ্ख͘આ໌Ͱ͖ͳ͍͜ͱ͸ɺࣗ෼΋׬શ ʹ͸ཧղͰ͖͍ͯͳ͍ɻ • ͨͱ͑ෳࡶͳॲཧͰ͋ͬͯ΋ɺ΍Γ͍ͨ͜ͱͷຊ࣭ ͸؆୯ʹઆ໌Ͱ͖Δ͸ͣɻ
  17. 19. Namespaces are one honking great idea - lets do

    more of those! • ʲ༁ʳͿΒ΅ʔͳΞΠσΟΞ໊લۭؒɺ΍ͬͺ͜Ε Ͱ͢Ͷɻ • ໊લۭؒ͸Pythonͷ࢓૊ΈΛࢧ͑Δߟ͑ํɻ • ʮ໊લۭؒͬͯԿʁʯͱٙ໰ʹࢥͬͨਓ͸ɺޙظͷ तۀͰઌੜʹ࣭໰͢Ε͹͖ͬͱஸೡʹڭ͑ͯ΋Β͑ ΔΑʂ
  18. A list of references • PEP 20 -- The Zen

    of Python | Python.org
 http://www.python.org/dev/peps/pep-0020/ • The Zen of Pythonʢ೔ຊޠ༁ʣ
 http://www.python.jp/Zope/articles/misc/zen • ΤΩεύʔτPythonϓϩάϥϛϯά
 http://www.amazon.co.jp/dp/4048686291 • ۭඈͿϞϯςΟɾύΠιϯ
 http://www.amazon.co.jp/dp/B000X431A4
  19. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠