Difference: FormattingGuideLines ( vs. 1)

Revision 12010/08/13 - Main.ParagMhashilkar

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="GlideinWMS"

Formatting the Code

Tips in a Nutshell

  • Use 4 spaces per indentation level.Never mix tabs and spaces.
  • Maximum Line Length should be preferably set to 79 or 80 characters
  • Separate top-level function and class definitions with two blank lines.
  • Method definitions inside a class are separated by a single blank line.
  • Extra blank lines may be used (sparingly) to separate groups of related functions.
  • Code in the core Python distribution should aways use the ASCII or Latin-1 encoding (a.k.a. ISO-8859-1). For Python 3.0 and beyond, UTF-8 is preferred over Latin-1
  • Imports should usually be on separate lines rather than comma separated.
  • Comments that contradict the code are worse than no comments.
  • Comments should be complete sentences.
  • Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code.
  • Use inline comments sparingly.
  • Follow sections in link above for Documentation Strings, Version Bookkeeping, Naming Conventions
  • Avoid extraneous whitespace in the following situations:
Situation Recommended Way Not Recommended
Immediately inside parentheses, brackets or braces.
spam(ham[1], {eggs: 2}) 
spam( ham[ 1 ], { eggs: 2 } ) 
Immediately before a comma, semicolon, or colon.
if x == 4: print x, y; x, y = y, x 
if x == 4 : print x , y ; x , y = y , x 
Immediately before the open parenthesis that starts the argument list of a function call.
spam(1)
spam (1)
Immediately before the open parenthesis that starts an indexing or slicing
dict['key'] = list[index]
dict ['key'] = list [index]
More than one space around an assignment (or other) operator to align it with another
x = 1
long_variable = 3
x            = 1
long_variable = 3
Always surround these binary operators with a single space on either side: assignment ( ), augmented assignment (+, -= etc.), comparisons ( =, <, >, , <>, <=, >=, in, not in, is, is not), Booleans (and, or, not)</pre>
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
def complex(real, imag=0.0):
    return magic(r=real, i=imag)
def complex(real, imag = 0.0):
    return magic(r = real, i = imag)
Compound statements (multiple statements on the same line) are generally discouraged.
if foo == 'blah':
    do_blah_thing()
do_one()
do_two()
do_three()
if foo == 'blah': do_blah_thing() do_one();
do_two(); do_three()
While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines! -
if foo == 'blah': do_blah_thing()
else: do_non_blah_thing()
try: something()
finally: cleanup()
do_one(); do_two(); do_three(long, argument,
                            list, like, this)
if foo == 'blah': one(); two(); three()
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback