pep8を読んでみよう

Download PEP8を読んでみよう

If you can't read please download the document

Upload: yoshitaka-tsubouchi

Post on 19-Jun-2015

1.092 views

Category:

Software


1 download

DESCRIPTION

第24回 Python東海 勉強会 発表資料

TRANSCRIPT

  • 1. PEP8 Python 24 2014524 @2box2bo

2. @2box2bo http://www.zumwalt.info/blog ( ) Python () 3. PEP 4. PEP Python Enhancement Proposal Python Python 5. PEP8 6. PEP8 PEP8 Style Guide for Python Code 7. "A Foolish Consistency is the Hobgoblin of Little Minds" 8. PEP8 9. Code lay-out 10. Indentation 11. 1 4 def long_function_name( var_one, var_two, var_three, var_four): print(var_one) 12. foo = long_function_name(var_one, var_two, var_three, var_four) foo = long_function_name( var_one, var_two, var_three, var_four) 13. my_list = [ 1, 2, 3, 4, 5, 6, ] result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', ) 14. # 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) 15. Tabs or Spaces? 16. ? Python3 Python2 17. Maximum Line Length 18. 79 docstring 72 99 72 ! ! # 72 ! def aaa(): print 7999 19. withassert 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()) 20. 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) 21. Blank Lines 22. class foo(object): # contents of foo ! ! class bar(object): # contents of bar 23. Source File Encoding 24. Python Python3utf8Python2ASCII PEP3131 25. import 26. import import import os import sys ! from sabprocess import Popen, PIPE 27. import import 1. import 2. import 3. /import import 28. Whitespace in Expressions and Statements 29. Pet Peeves 30. - - - # 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]) 31. - # OK x = 1 y = 2 long_variable = 3 ! #NG x = 1 y = 2 long_variable = 3 32. Other Recommendations 33. 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) 34. = # 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) 35. 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) 36. Comments 37. The Elements of Style 120% 38. Block Comments 39. #1 40. Inlines Commnets 41. 2 #1 42. Documentation Strings 43. (docstring) PEP257 "" 1scstring""" 44. 45. PEP PythonPython 46. 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 47. PEP8 azusa http://memo.sanographix.net/post/82160791768 48.