Save 37% off PRO during our Black Friday Sale! »

pep8を読んでみよう.pdf

246d2365fbcf6685ff7eaa084d1361ea?s=47 2bo
May 25, 2014

 pep8を読んでみよう.pdf

246d2365fbcf6685ff7eaa084d1361ea?s=128

2bo

May 25, 2014
Tweet

Transcript

  1. PEP8 ΛಡΜͰΈΑ͏ Python౦ւ ୈ24ճษڧձ 2014೥5݄24೔ @2box2bo

  2. ࣗݾ঺հ • ໊લɿ௶಺ ༝޹
 @2box2bo
 • ϒϩάɿhttp://www.zumwalt.info/blog • ͓࢓ࣄɿ஍ํ͍͑͋͋͢ʔͷΠϯϑϥ԰ •

    ॴଐɿ(” ՞ਊ ՞)”΢ΟʔϯͳϢʔβʔձ
 Python౦ւ • ࠷ۙͷ͋Εɿ਀໊͸େৎ෉Ͱ͢ʂ
 (´ʀωʀʆ)ŲƄƂŕ

  3. None
  4. PEP

  5. PEP ͱ͸ͳΜͧʁ • ʮPython Enhancement Proposalʯͷུ
 ೔ຊޠ༁͢ΔͱʮPython֦ுఏҊʯ • Python ίϛϡχςΟʹ৘ใఏڙ΍৽ػೳɾ


    ϓϩηεɾ؀ڥ౳Λઆ໌͢ΔͨΊͷઃܭॻ • ٕज़తͳ࢓༷ͱͦͷػೳ͕ඞཁͳ࿦ཧతͳ
 ཧ༝Λఏڙ͢Δ
  6. PEP8

  7. PEP8 ͱ͸ͳΜͧʁ • PEPͷ8൪໨ 
 λΠτϧ͸ʮStyle Guide for Python Codeʯ

    • ಺༰͸ίʔσΟϯάελΠϧͷΨΠυ • ೔ʑɺ࣌୅ʹ͋ͬͨ෺ʹมΘ͍ͬͯ͘ • ࣗ෼͕ॴଐ͍ͯ͠Δ૊৫ͷίʔσΟϯάελΠϧͱ ڝ߹ͨ͠Βࣗ૊৫ͷίʔσΟϯάελΠϧΛ༏ઌ
  8. "A Foolish Consistency is the Hobgoblin of Little Minds"

  9. ۪͔ͳҰ؏ੑ͸ڱ͍৺͕Խ͚ͨ ΋ͷͰ͋Δ • ίʔυ͸ॻ͔ΕΔΑΓ΋ͣͬͱଟ͘ಡ·ΕΔ • ελΠϧΨΠυ͸Ұ؏ੑͷͨΊʹ͋Δ • Ұ൪ॏཁͳࣄ͸ɺҰ؏ੑΛఘΊΔɺελΠϧ ΨΠυΛద༻͠ͳ͍ λΠϛϯάΛ஌Δࣄ

    • PEP8 ʹै͏ͨΊʹޙํޓ׵ੑΛյ͢͜ͱ͸ઈ ରʹ͠ͳ͍
  10. Code lay-out

  11. Indentation

  12. Πϯσϯτ̍ • 1 ஈ֊ͷΠϯσϯτʹ͸ 4 ͭͷεϖʔεΛ࢖͏ • ܧଓߦͷΠϯσϯτ͸ͦͷ࣍ͷΠϯσϯτͱ ۠ผͰ͖ΔΑ͏ʹ͢Δ def

    long_function_name( var_one, var_two, var_three, var_four): print(var_one)
  13. Πϯσϯτ̎ • ΧοίʹΑΔ҉໧ͷߦܧଓΛ࢖͍ͬͯΔ৔߹ɺ ॎʹἧ͑Δ͔ʮ௻ΓԼ͛ΠϯσϯτʯΛ࢖͏ • ʮ௻ΓԼ͛ΠϯσϯτʯΛ࢖͍ͬͯΔ৔߹͸ ࠷ॳͷߦʹҾ਺Λஔ͔ͳ͍͜ͱɻ foo = long_function_name(var_one,

    var_two, var_three, var_four) foo = long_function_name( var_one, var_two, var_three, var_four)
  14. Πϯσϯτ̏ • ෳ਺ߦʹ౉ΔΧοίͷด͡Χοί͸ͦͷߦͷ
 ࠷ॳͷۭനͰͳ͍จࣈͷΠϯσϯτʹἧ͑Δ • ΋͘͠͸ɺͦͷෳ਺ߦͷཁૉ͕։࢝ͨ͠
 Πϯσϯτʹἧ͑Δ my_list = [

    1, 2, 3, 4, 5, 6, ] result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )
  15. Πϯσϯτ̐ # NG 1 foo = long_function_name(var_one, var_two, var_three, var_four)

    ! # NG 2 def long_function_name( var_one, var_two, var_three, var_four): print(var_one) ʮ௻ΓԼ͛ΠϯσϯτʯΛ࢖͏৔߹͸ ࠷ॳͷߦʹҾ਺Λॻ͍ͯ͸͍͚ͳ͍ ࣍ͷΠϯσϯτͱ۠ผͰ͖ΔΑ͏ʹ͢Δ
  16. Tabs or Spaces?

  17. λϒ͔εϖʔε͔? • Πϯσϯτʹ͸εϖʔεΛਪ঑ • λϒΛ࢖͍͍ͬͯͷ͸طʹλϒͰΠϯσϯτ͞ Ε͍ͯΔίʔυͱҰ؏ੑΛอͭ৔߹ͷΈ • Python3 Ͱ͸Πϯσϯτʹλϒͱεϖʔε
 ྆ํ࢖͏͜ͱΛڐՄ͠ͳ͍

    • Python2 ༻ͷίʔυͰ྆ํ࢖ΘΕ͍ͯΔ৔߹͸
 εϖʔεͷΈʹม׵͢Δ
  18. Maximum Line Length

  19. ߦͷ௕͞ͷ࠷େ஋̍ • શͯͷߦ͸79จࣈҎ಺ • docstring ΍ίϝϯτ͸72จࣈ • νʔϜͰ߹ҙ͕औΕΕ͹99จࣈ·Ͱ૿΍ͯ͠΋ྑ͍ ‘’’ ࠷େ72จࣈ

    ! ‘’’ ! # ࠷େ72จࣈ ! def aaa(): print “ඪ४తʹ͸79จࣈɺ࠷େ99จࣈ”
  20. ߦͷ௕͞ͷ࠷େ஋̎ • ߦ͕௕͍৔߹͸ΧοίͰͷ҉໧తͳߦܧଓ͔όο ΫεϥογϡΛ࢖ͬͯॻ͘ • ΧοίͰͷߦܧଓΛ༏ઌͯ͠ॻ͘ • withɺassertͰ͸όοΫεϥογϡΛ࢖༻͢Δ with open('/path/to/some/file/you/want/to/read')

    as file_1, \ open('/path/to/some/file/being/written', 'w') as file_2: file_2.write(file_1.read())
  21. ߦͷ௕͞ͷ࠷େ஋̏ • ܧଓߦΛద੾ʹΠϯσϯτ͢Δ • ߲̎ԋࢉࢠͰվߦ͢Δ৔߹͸ԋࢉࢠͷޙΖͰ
 վߦ͢Δͷ͕ਪ঑ class Rectangle(Blob): ! def

    __init__(self, width, height, color='black', emphasis=None, highlight=0): if (width == 0 and height == 0 and color == 'red' and emphasis == 'strong' or highlight > 100): raise ValueError("sorry, you lose") if width == 0 and height == 0 and (color == 'red' or emphasis is None): raise ValueError("I don't think so -- values are %s, %s" % (width, height)) Blob.__init__(self, width, height, color, emphasis, highlight)
  22. Blank Lines

  23. ۭߦ • τοϓϨϕϧͷؔ਺ఆٛͱΫϥεఆٛ͸ۭനߦ̎ͭͰ ۠੾Δ • Ϋϥε಺ͷϝιουఆٛ͸ۭനߦ̍ͭ • ࿦ཧతͳάϧʔϓΛ෼͚ΔҝʹۭനߦΛ࢖͏͜ͱ΋Մೳ class foo(object):

    # contents of foo ! ! class bar(object): # contents of bar
  24. Source File Encoding

  25. ιʔεϑΝΠϧͷΤϯίʔσΟϯά • Python഑෍෺ͷίʔυ͸ৗʹ
 Python3͸utf8ɺPython2͸ASCIIͰ͋Δ͜ͱ • جຊతʹ͸ӳޠͰॻ͘ • ৄࡉ͸PEP3131 ࢀর

  26. import

  27. Πϯϙʔτ̍ • importจ͸̍ߦͮͭʹ෼͚Δ͜ͱ • ͨͩ͠Ϋϥε໊౳Λ·ͱΊͯimport͸Մೳ import os import sys !

    from sabprocess import Popen, PIPE
  28. Πϯϙʔτ̎ • importจ͸ίʔυͷઌ಄ʹॻ͘ • importจ͸࣍ͷॱ൪ͰάϧʔϓԽ͢Δ
 1. ඪ४ϥΠϒϥϦͷ import
 2. αʔυύʔςΟϥΠϒϥϦͷimport


    3. ϩʔΧϧΞϓϦ/ϥΠϒϥϦಠࣗͷimport • ֤importͷάϧʔϓ͸ۭനߦͰ۠੾Δ
  29. Whitespace in Expressions and Statements

  30. Pet Peeves

  31. ͍ͭ΋ͷจ۟ͷλω̍ • ҎԼͷΑ͏ͳ༨ܭͳεϖʔεΛ࢖Θͳ͍
 - Χοίͷ಺ଆ
 
 - ΧϯϚɺηϛίϩϯɺίϩϯͷखલ
 
 -

    ؙΧοί΍֯Χοίͷखલ # OK spam(ham[1], {eggs: 2}) # NG spam( ham[ 1 ], { eggs: 2 } ) # OK if x == 4: print x, y; x, y = y, x # NG if x == 4 : print x , y ; x , y = y , x # OK dict[‘key’] = spam(list[index]) # NG dict [‘key’] = spam (list [index])
  32. ͍ͭ΋ͷจ۟ͷλω̎ - ଞͷߦͱἧ͑ΔͨΊʹ̎ͭҎ্ͷεϖʔε
 # OK x = 1 y =

    2 long_variable = 3 ! #NG x = 1 y = 2 
long_variable = 3 ※ ͜ͷهड़ํ๏ʹ͸ࢍ൱྆࿦͋Γ
  33. Other Recommendations

  34. ͦͷଞͷਪ঑̍ • ҎԼͷ2߲ԋࢉࢠͷࠨӈʹ͸ඞͣεϖʔε
 ୅ೖ (=)ɾෳ߹୅ೖ (+=, -= etc.)
 ൺֱ (==,

    <, >, !=, <>, <=, >=, in, not in, is, is not)
 ࿦ཧ (and, or, not) # OK a = i + 1 b = x*2 - y*y c = (a+b) * (a-b) ! # NG a = i+1 b = x * 2 - y * y c = (a + b) * (a - b)
  35. ͦͷଞͷਪ঑̎ • ΩʔϫʔυҾ਺΍Ҿ਺ͷσϑΥϧτ஋ͷ৔߹
 =ʹ͸લޙͷεϖʔεΛೖΕͳ͍ # OK def complex(real, imag=0.0): return

    magic(r=real, i=imag) ! # NG def complex(real, imag = 0.0): return magic(r = real, i = imag)
  36. ͦͷଞͷਪ঑̏ • ෳ਺ͷจΛ1ͭͷߦʹॻ͘͜ͱ͸ඇਪ঑ • খ͍͞จͰ͋Ε͹ڐ͞ΕΔ৔߹΋͋Δ # OK if foo ==

    'blah': do_blah_thing() do_one() do_two() do_three() ! # Rather not if foo == 'blah': do_blah_thing() do_one(); do_two(); do_three() ! # NG do_one(); do_two(); do_three(long, argument, list, like, this)
  37. Comments

  38. ίϝϯτ • ίʔυͱໃ६͍ͯ͠Δίϝϯτ͸ίϝϯτ͕ͳ͍ ΑΓ΋ѱ͍ɻৗʹ࠷৽ʹอͭ • ίϝϯτ͕จͷ৔߹ɺ࠷ॳͷจࣈ͸େจࣈʹ͢Δ • ӳޠΛॻ͘৔߹ʮThe Elements of

    Styleʯʹै͏ • 120% ଞݴޠݍͷਓ͕ίʔυΛಡ·ͳ͍ͱ֬৴͕
 ແ͍ݶΓӳޠͰίϝϯτΛॻ͘
  39. Block Comments

  40. ϒϩοΫίϝϯτ • ϒϩοΫίϝϯτ͸ಉ͡ϨϕϧͷΠϯσϯτͷ ίʔυʹର͢Δίϝϯτ • ϒϩοΫίϝϯτ͸#ͱ1ͭͷεϖʔεͰ࢝·Δ

  41. Inlines Commnets

  42. ΠϯϥΠϯίϝϯτ • ΠϯϥΠϯίϝϯτ͸ίʔυͱಉ͡ߦʹॻ͔ΕΔ ίϝϯτ • ίʔυͱΠϯϥΠϯίϝϯτͷؒ͸গͳ͘ͱ΋
 2ͭͷεϖʔεͰ۠੾ΓɺΠϯϥΠϯίϝϯτ͸
 #ͱ1ͭͷεϖʔεͰ࢝·Δ

  43. Documentation Strings

  44. υΩϡϝϯτจࣈྻ • ྑ͍υΩϡϝϯτจࣈྻ(docstring)ͷॻ͖ํ͸
 PEP257ʹॻ͔Ε͍ͯΔ • શͯͷϞδϡʔϧɺΫϥεɺؔ਺ɺϝιουʹॻ͘ • ຤ඌʹͭ͘”""͸ಠཱͨ͠ߦͱͯ͠ॻ͖ɺۭനߦΛ
 ຤ඌͷલʹೖΕΔͷ͸ਪ঑͞ΕΔ •

    1ߦͷscstring͸ɺ຤ඌͷ"""Λಉ͡ߦʹॻ͚Δ
  45. ·ͱΊ

  46. ·ͱΊ • ಡΈ΍͍͢ίʔυΛॻ͖·͠ΐ͏ • ͨ·ʹ͸PEPΛಡΜͰΈΔͱ໘ന͍ • ͨͩ͠ɺӳޠྗ͕͍Δ • Python౦ւͷൃදͰॳΊͯPythonͷίʔυΛൃ දͨ͠

  47. ࢀߟURL • http://www.tdoc.info/PEP-ja/ • https://dl.dropboxusercontent.com/u/555254/ pep-0008.ja.html • http://www.lifewithpython.com/2013/01/pep-8-style-guide- for-python-code.html •

    http://legacy.python.org/dev/peps/pep-0008/ • http://sphinx-users.jp/articles/pep1.html
  48. ࠷ޙʹ • ҙ༁ɺ୺ંͬͨจɾ಺༰͕ଟʑ͕͋Γ·͢ɻ • PEP8ʹ͸όʔδϣϯ؅ཧͱ໋໊نଇ͕͋Γ·͕͢ ୺ંΓ·ͨ͠ɻ • ؒҧͬͯͨΒ͝ΊΜͳ͍͞ɻ • azusaςϯϓϨʔτΛ࢖͍·ͨ͠ɻ


    http://memo.sanographix.net/post/82160791768
  49. ͓·͚