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

pep8を読んでみよう.pdf

2bo
May 25, 2014

 pep8を読んでみよう.pdf

2bo

May 25, 2014
Tweet

More Decks by 2bo

Other Decks in Technology

Transcript

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

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

  2. PEP

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


    ϓϩηεɾ؀ڥ౳Λઆ໌͢ΔͨΊͷઃܭॻ • ٕज़తͳ࢓༷ͱͦͷػೳ͕ඞཁͳ࿦ཧతͳ
 ཧ༝Λఏڙ͢Δ
  4. PEP8 ͱ͸ͳΜͧʁ • PEPͷ8൪໨ 
 λΠτϧ͸ʮStyle Guide for Python Codeʯ

    • ಺༰͸ίʔσΟϯάελΠϧͷΨΠυ • ೔ʑɺ࣌୅ʹ͋ͬͨ෺ʹมΘ͍ͬͯ͘ • ࣗ෼͕ॴଐ͍ͯ͠Δ૊৫ͷίʔσΟϯάελΠϧͱ ڝ߹ͨ͠Βࣗ૊৫ͷίʔσΟϯάελΠϧΛ༏ઌ
  5. Πϯσϯτ̐ # 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) ʮ௻ΓԼ͛ΠϯσϯτʯΛ࢖͏৔߹͸ ࠷ॳͷߦʹҾ਺Λॻ͍ͯ͸͍͚ͳ͍ ࣍ͷΠϯσϯτͱ۠ผͰ͖ΔΑ͏ʹ͢Δ
  6. ߦͷ௕͞ͷ࠷େ஋̏ • ܧଓߦΛద੾ʹΠϯσϯτ͢Δ • ߲̎ԋࢉࢠͰվߦ͢Δ৔߹͸ԋࢉࢠͷޙΖͰ
 վߦ͢Δͷ͕ਪ঑ 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)
  7. ͍ͭ΋ͷจ۟ͷλω̍ • ҎԼͷΑ͏ͳ༨ܭͳεϖʔεΛ࢖Θͳ͍
 - Χοίͷ಺ଆ
 
 - ΧϯϚɺηϛίϩϯɺίϩϯͷखલ
 
 -

    ؙΧοί΍֯Χοίͷखલ # 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])
  8. ͍ͭ΋ͷจ۟ͷλω̎ - ଞͷߦͱἧ͑ΔͨΊʹ̎ͭҎ্ͷεϖʔε
 # OK x = 1 y =

    2 long_variable = 3 ! #NG x = 1 y = 2 
long_variable = 3 ※ ͜ͷهड़ํ๏ʹ͸ࢍ൱྆࿦͋Γ
  9. ͦͷଞͷਪ঑̍ • ҎԼͷ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)
  10. ͦͷଞͷਪ঑̎ • ΩʔϫʔυҾ਺΍Ҿ਺ͷσϑΥϧτ஋ͷ৔߹
 =ʹ͸લޙͷεϖʔεΛೖΕͳ͍ # 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)
  11. ͦͷଞͷਪ঑̏ • ෳ਺ͷจΛ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)