Python Lines and indentation

How to create a block in python?

  • No braces to indicate blocks of code for class and function or flow control.
  • Blocks of code are represented by line indentation.
  • Number of spaces in indentation is different (or variable), but all statements in the block must be indented the same number of spaces.

Valid block:

if True:
   print "true"
else:
 print "false"

Python program for valid block:

#!/usr/bin/python                                                               
                                                                                
import random                                                                   
import time                                                                     
import threading                                                                
                                                                                
def display_message(count):                                                     
    random_sec = random.randint(1,4)                                            
    print "thread %d is going to sleep for %d seconds" % (count, random_sec)    
    time.sleep(random_sec)                                                      
    print "thread %d is resuming back" % count                                  
                                                                                
for count in range(5):                                                          
    thr = threading.Thread(target=display_message, args=(count,))               
    thr.start()                                                                 
Output:
$ python thread_test1.py 
thread 0 is going to sleep for 2 seconds
thread 1 is going to sleep for 3 seconds
thread 2 is going to sleep for 3 seconds
thread 3 is going to sleep for 2 seconds
thread 4 is going to sleep for 1 seconds

thread 4 is resuming back
thread 0 is resuming back
thread 3 is resuming back
thread 1 is resuming back
 thread 2 is resuming back

Invalid block

Python interpreter generates error.
if True:
    print "line1"
    print "line2"
else:
    print "line3"
  print "line4"

Python program for invalid block:

#!/usr/bin/python                                                               
                                                                                
import random                                                                   
import time                                                                     
import threading                                                                
                                                                                
def display_message(count):     
    #indentation is not same in all lines of this method.                                                
    random_sec = random.randint(1,4)                                            
      print "thread %d is going to sleep for %d seconds" % (count, random_sec)  
     time.sleep(random_sec)                                                     
     print "thread %d is resuming back" % count                                 
                                                                                
for count in range(5):                                                          
    thr = threading.Thread(target=display_message, args=(count,))               
    thr.start()                                                                 
Python interpreter error:
$ python thread_test1.py 
  File "thread_test1.py", line 9
    print "thread %d is going to sleep for %d seconds" % (count, random_sec)
    ^
IndentationError: unexpected indent

Python Coding Style

  • PEP 8 is the python style guide and promotes a better readable coding style. most projects are adhere to PEP8.
  • Wherever indentation is required, use 4-space indentation
  • Don't use tabs
  • Each line don't exceed 79 charaters, wrap lines less than 79 chars.
  • Use documentation strings (docstrings).
  • blank lines need to be used to differentiate functions and classes, and blocks of code inside functions.
  • Wherever complex logic, place the comments to understand the code logic.
  • Space should be included before and after operators but not inside brackets (a+b), and after commas.
  • Naming classes and functions in consistent, CamelCase for classes and lower_case_with_underscores for functions and methods.
  • ASCII encoding works best, don't use fancy encoding

How to ensure that python code is followed above coding styles?

tox command is available to check the python coding style issues. virtualenv environment
$ . venv/bin/activate
[venv]$cd project-folder
[venv]$tox -e pep8
Output will be displayed like below,
pep8 develop-inst-nodeps: folder@4e65b339c7cf303269ceef34b1e550c0eafa9dae#egg=project-folder,typing==3.6.2,unicodecsv==0.14.1,unittest2==1.1.0,urllib3==1.22,warlock==1.2.0,websocket-client==0.44.0,wrapt==1.10.11
pep8 runtests: PYTHONHASHSEED='2009495220'
pep8 runtests: commands[0] | flake8
pep8 runtests: commands[1] | bash -c tools/check_duplicate_jinja_blocks.sh
_____________________________________________________________________________________________________ summary _____________________________________________________________________________________________________
  pep8: commands succeeded
  congratulations :)

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^