0% found this document useful (0 votes)
4K views1 page

PEP-8 Cheatsheet (2009)

This document provides guidelines for writing Python code that follows PEP 8 style conventions. It recommends using lowercase names with underscores for modules, packages, and variables. Functions and method names should be lowercase with underscores, and self should always be the first argument of methods. Comments, docstrings, and empty lines are used to enhance readability. Class names use CapWords convention.

Uploaded by

Veeraj Shenoy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views1 page

PEP-8 Cheatsheet (2009)

This document provides guidelines for writing Python code that follows PEP 8 style conventions. It recommends using lowercase names with underscores for modules, packages, and variables. Functions and method names should be lowercase with underscores, and self should always be the first argument of methods. Comments, docstrings, and empty lines are used to enhance readability. Class names use CapWords convention.

Uploaded by

Veeraj Shenoy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PEP-8 cheatsheet - http://wwd.

ca/blog/2009/07/09/pep-8-cheatsheet/

#! /usr/bin/en p!thon # -"- coding: ut#-8 -"$$$%his &odule's docstring su&&ar! line. %his is a &ulti-line docstring. Paragraphs are separated with blan( lines. )ines con#or& to 79-colu&n li&it. *odule and pac(ages na&es should be short+ lower,case,with,underscores. -ee http://www.p!thon.org/de /peps/pep-0008/ #or &ore PEP-8 details and http://wwd.ca/blog/2009/07/09/pep-8-cheatsheet/ #or an up-to-date ersion o# this cheatsheet. $$$ i&port os i&port s!s
i&port so&e,third,part!,lib i&port so&e,other,third,part!,lib i&port !our,local,stu## i&port &ore,local,stu## i&port dont,i&port,two+ &odules,in,one,line ,a,global, ar 3 2 ,b,global, ar 3 4 1,5/2-%12% 3 'ugh.'

l 3 A / 3 2 . 3 4

# so&e e=a&ples o# how to wrap code to con#or& to 79-colu&ns li&it: de# ,,init,,7sel#+ width+ height+ color3'blac('+ e&phasis32one+ highlight308: i# width 33 0 and height 33 0 and B color 33 'red' and e&phasis 33 'strong' or B highlight C A00: raise @alueError7$sorr!+ !ou lose$8 i# width 33 0 and height 33 0 and 7color 33 'red' or e&phasis is 2one8: raise @alueError7$. don't thin( so -- alues are Ds+ Ds$ D 7width+ height88 :lob.,,init,,7sel#+ width+ height+ color+ e&phasis+ highlight8 # e&pt! lines within &ethod to enhance readabilit!? no set rule short,#oo,dict 3 E'loooooooooooooooooooong,ele&ent,na&e': 'cat'+ 'other,ele&ent': 'dog'F
long,#oo,dict,with,&an!,ele&ents 3 E '#oo': 'cat'+ 'bar': 'dog' F

# .*P/0%12%!

# so it won't get i&ported b! '#ro& #oo i&port "'

# 2 e&pt! lines between top-le el #uncs 6 classes de# so&e,#unction78: pass


class 9oo:ar7ob;ect8: $$$<rite docstrings #or 1)) public classes+ #uncs and &ethods.

# A e&pt! line between in-class de#'ns de# #oo,&ethod7sel#+ =+ !32one8: $$$*ethod and #unction na&es are lower,case,with,underscores. 1lwa!s use sel# as #irst arg. $$$ i# = 33 >: # = is blue G33 H-E9H) A-liner co&&ent =+ ! 3 !+ = # in erse = and ! G33 H-E)E-- 5/**E2% dictI'(e!'J 3 dictIinde=J 3 E=: 2+ 'cat': 'not a dog'F c 3 7a 6 b8 " 7a - b8
Kclass&ethod de# bar7cls8: $$$Hse cls!$$$ pass # a 79-char ruler: #24>LM789AA24>LM7892A24>LM7894A24>LM789>A24>LM789LA24>LM789MA24>LM7897A24>LM789

5lass and e=ception na&es are 5ap<ords. $$$


a 3 2 b 3 > ,internal, ariable 3 4 class, 3 '#oo' # trailing underscore to a oid con#lict with builtin

# this will trigger na&e &angling to #urther discourage use #ro& outside # this is also er! use#ul i# !ou intend !our class to be subclassed+ and # the children &ight also use the sa&e ar na&e #or so&ething else? e.g. # #or si&ple ariables li(e 'a' abo e. 2a&e &angling will ensure that # "!our" a and the children's a will not collide. ,,internal, ar 3 > # 2E@E0 use double leading and trailing underscores #or !our own na&es ,,nooooooodontdoit,, 3 0 # don't call an!thing:

You might also like