Re: programmingblind Digest V2 #48

  • From: Michael Whapples <mwhapples@xxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Mon, 18 Feb 2008 12:22:32 +0000

In general in python you indent where ever you have a block normally
surrounded by braces (the {} characters, eg. classes, functions/methods,
ifs, loops, etc). Each indent continues from the indent level at which
you start the block definition, and is usually a couple of spaces or a
tab (eg. you may have a class which has its contents indented by one
tab, this class may contain a method whose contents is indented by
another tab (IE. two tabs from the beginning of the line), which may
contain a loop whose contents is indented by yet a further tab (IE.
three tabs from the beginning of the line), and so on. Here is a code
example of the above structure:

class MyClass:
    x = 2
    def __init__(self):
        # Print the value of x 5 times
        for y in range(0,4):
            print x
        # Now print a complete to the screen
        print "complete"

). One thing to note in the above example, nothing other than the lack
of an indentation marks the end of the block. This means that if you
have a block which should be empty, use the pass statement. An example
of this might be in exception handling where upon an exception you may
wish to ignore it and continue. Th following code example shows this:


You may wish to consider how various editors do their indentations for
python. I think quite a number automatically indent after the correct
definition statements which begin a block, and stop when you press enter
twice (IE. leave a blank line).

I have just thought of a possibly simpler rule for when to indent, if
you finish the line with a colon (:) then indent everything which
belongs to its block.

Michael Whapples
> From: "Jaffar@xxxxxxxxxxxxx" <jaffar@xxxxxxxxxxxxx>
> Subject: python, indentation and confusion
> Date: Sun, 17 Feb 2008 23:46:27 +0800
> Hi Gang.  My boss who was originally a c++ buff has now been converted to 
> python.  As a result, me and my colleagues have had to learn a new 
> programming language to suit my boss's latest preference.  Surprisingly, the 
> learning curve for the python language is not that steep when compared to c, 
> c++ or java, my personal opinion of course.  The only difficulty i've had to 
> face is the indentation rules as is laid out in python's coding convention.  
> While my sighted colleagues aren't really concerned about this, I get all 
> muddled up especially when it comes to coding of win32 gui because my code 
> always fails through improper indentation.  This is true whether i am coding 
> with native python or with the Tkinter or the wxPython toolkits.  The thing 
> is that i don't encounter such indentation problems when i am writing native 
> python scripts.  So, knowing that there are some among you who have, and 
> still are coding in python, what are the rules of thumb when it comes to 
> properly inden
>  ng python code?  All help on this will be very much appreciated thanks.
> By the way, having spent the last 1 and ahalf months studying python 
> scripting and programming, i am beginning to understand why my boss fell for 
> the language, at least in terms of productivity and reliability.  Cheers!

View the list's information and change your settings at

Other related posts:

  • » Re: programmingblind Digest V2 #48