41
h"p://blogs.uct.ac.za/gallery/669/PowerPoint.gif 1

PEP Presentation for PythonKC

Embed Size (px)

Citation preview

Page 2: PEP Presentation for PythonKC

Zen  of  Python  (PEP  20)

2

Page 3: PEP Presentation for PythonKC

>>>  import  this

3

h"p://www.bolgernow.com/blog/wp-­‐content/uploads/2010/05/old-­‐lady-­‐middle-­‐finger.jpg

3

Page 4: PEP Presentation for PythonKC

BeauLful  is  be"er  than  ugly

h"p://www.dailystab.com/blog/wp-­‐content/uploads/2009/06/ugly.jpg

4

Page 5: PEP Presentation for PythonKC

Explicit  is  be"er  than  implicit

h"p://cache2.artprinLmages.com/p/LRG/19/1922/GWT9D00Z/art-­‐print/parental-­‐advisory-­‐explicit-­‐lyrics.jpg

5

Page 6: PEP Presentation for PythonKC

Simple  is  be"er  than  complex

h"p://musikality.net/wp-­‐content/uploads/simplicity.jpg

6

Page 7: PEP Presentation for PythonKC

Complex  is  be,er  than  complicated

h"p://vipdicLonary.com/img/life_geXng_complicated.jpg

7

Page 10: PEP Presentation for PythonKC

Readability  Counts

h"p://www.voidspace.org.uk/python/weblog/images/readability.gif

10

Page 11: PEP Presentation for PythonKC

Special  cases  aren't  special  enough  to  break  the  rules.

h"p://www.hornblend.com/wp-­‐content/uploads/2010/08/duh.jpg

11

Page 12: PEP Presentation for PythonKC

Although  pracLcality  beats  purity.

h"p://vilain.net/files/pracLcality.jpg

12

Page 13: PEP Presentation for PythonKC

Errors  should  never  pass  silently.

h"p://media02.hongkiat.com/funny_error_messages/funny-­‐bsod-­‐02.jpg

13

Page 14: PEP Presentation for PythonKC

Unless  explicitly  silenced.

h"p://thenextweb.com/files/2010/08/Hear.No_.Evil_.jpg

14

Page 15: PEP Presentation for PythonKC

In  the  face  of  ambiguity,  refuse  the  tempta>on  to  guess.

h"p://www.doublejourney.com/wp-­‐content/ambiguity.jpg

15

Page 16: PEP Presentation for PythonKC

There  should  be  one-­‐-­‐  and  preferably  only  one  -­‐-­‐obvious  way  to  do  it.

h"p://www.motorcycles-­‐ok.com/mokpics/x/obvious.jpg

16

Page 17: PEP Presentation for PythonKC

Although  that  way  may  not  be  obvious  at  first  unless  you're  Dutch.

h"p://ew.cachefly.net/snm/images/nm/flags/nl-­‐lgflag.gif

Dutch  Flag

17

Page 18: PEP Presentation for PythonKC

Now  is  be"er  than  never.

h"ps://lh6.googleusercontent.com/_zGnH6HWaAi0/TY_3ysjUkOI/AAAAAAAAKSI/S168q0DA9vs/never_regret.jpg

18

Page 19: PEP Presentation for PythonKC

Although  never  is  oFen  be,er  than  *right*  now.

h"p://idioelashback.files.wordpress.com/2010/02/van_halen_logo.jpg

Why,  Eddie?

19

Page 20: PEP Presentation for PythonKC

If  the  implementa>on  is  hard  to  explain,  it's  a  bad  idea.

h"p://img256.imageshack.us/img256/8379/magnets.jpg

20

Page 21: PEP Presentation for PythonKC

If  the  implementa>on  is  easy  to  explain,  it  may  be  a  good  idea.

h"p://images.cheezburger.com/completestore/2011/6/11/6c21f0de-­‐4f50-­‐4194-­‐94bd-­‐10ba19b77d8f.jpg

21

Page 22: PEP Presentation for PythonKC

Namespaces  are  one  honking  great  idea  -­‐-­‐  let's  do  more  of  those!

h"p://1.bp.blogspot.com/_GfY7dNRDwYI/TUHlJyG7YKI/AAAAAAAAArU/yk-­‐RtF96ZWM/s1600/ponies-­‐and-­‐rainbows.jpg

22

Page 23: PEP Presentation for PythonKC

PEP 8

Why a Style Guide matters.

23

Page 25: PEP Presentation for PythonKC

What  is  a  Style  Guide?

h"p://intheknow.lordandtaylor.com/tag/fall-­‐2011/

25

Page 26: PEP Presentation for PythonKC

SecLons

• Lay-­‐out• Imports• Whitespace• Comments• Document  Strings• Version  Bookkeeping• Naming  Conven>ons• Programming  Recommenda>ons

26

26

Page 27: PEP Presentation for PythonKC

Lay  Out

• 4  spaces– Unless  you’re  working  on  some  old  stuff  that  is  already  using  8  spaces

• Ver>cal  Alignment– Either  align  with  opening  delimiter– Or  use  extra  indenta>on

27

Page 28: PEP Presentation for PythonKC

Lay  Out

• Tabs  and  Spaces– Never  mix  the  two  (like  liquor  and  beer)– If  in  doubt,  go  with  spaces,  only.

• Maximum  Line  Length– 79  char.    – Use  the  implicit  line  wrap  within  parenthesis

28

28

Page 30: PEP Presentation for PythonKC

Lay  Out

• Blank  Lines– 2  lines  between  top  level  func>on  and  class  defini>on

– Use  blank  lines  to  logically  group  sec>ons– It’s  not  MLA.    Don’t  double-­‐space  your  code.  

• Encodings– Core  code  =  ASCII  or  La>n-­‐1– Code  >  3.0  should  use  UTF-­‐8

30

30

Page 31: PEP Presentation for PythonKC

Lay  Out

31

Page 32: PEP Presentation for PythonKC

Imports

• One  import  per  line• Put  at  the  top  of  the  document  aFer  comments  and  doc  strings– First  Standard  Library– Then  related  third-­‐party  imports– Last,  applica>on  /  library  specific  imports

32

Page 33: PEP Presentation for PythonKC

Whitespace

Too  much  is  too  much.

h"p://www.alistapart.com/d/whitespace/whitespace.jpg

33

Page 34: PEP Presentation for PythonKC

Whitespace

• Don’t  get  cute  and  add  whitespace  within  parenthesis  or  brackets,  around  binary  operators.–Write  it  how  you  would  normally  write  it

• Do  use  spaces  around  arithme>c  operators–Math  is  hard  as  it  is,  don’t  make  it  harder  my  squishing  things  together...

34

34

Page 35: PEP Presentation for PythonKC

Whitespace  

• Don’t  put  compound  statements  on  one  line.• Spread  that  stuff  out  so  it  is  easy  to  find  the  various  clauses  and  stuff.

35

35

Page 36: PEP Presentation for PythonKC

Comments

• Comments  should  match  the  code.• If  there  is  a  conflict,  change  one  of  them!• Comments  should  be  complete  sentences,  wri,en  in  English.

• Lean  towards  using  block  comments.• Use  inline  comments  sparingly.• Don’t  be  the  obvious  troll.

36

Page 37: PEP Presentation for PythonKC

Doc  Strings

• Use  them!• PEP  257

h"p://catmacros.files.wordpress.com/2010/05/itoldyouno"odothat.jpg

37

Page 38: PEP Presentation for PythonKC

Naming  ConvenLons

• Single  Lower  (d)• Single  Upper  (W)• lowercase• lower_with_underscore• UPPERCASE• UPPER_WITH_UNDER• CamelCase• mixedCase

38

Page 39: PEP Presentation for PythonKC

Naming  ConvenLons  

• Avoid:– ‘l’,  ‘O’,  ‘I’

• Modules  should  be  short,  lowercase• Classes  in  CamelCase• Func>ons  should  be  lower_with_underscores• Constants  should  be  UPPER_WITH_UNDER

39

39

Page 40: PEP Presentation for PythonKC

Programming  RecommendaLons

• Thing  about  the  other  guys.• Don’t  use  equality  when  comparing  singletons• “if  X  is  not  None”  is  not  the  same  as  “if  X”

40

Page 41: PEP Presentation for PythonKC

h"p://thecoredump.org/images/interesLng-­‐man-­‐powerpoint.jpg

41