[pythonvis] Re: Yet another indenting explanation

  • From: Joseph Lee <joseph.lee22590@xxxxxxxxx>
  • To: pythonvis@xxxxxxxxxxxxx
  • Date: Wed, 14 May 2014 22:03:45 -0700

Hi,
For classes and functions, you'd enclose the content/subroutine one level below the class/function declaration.
Cheers,
Joseph

----- Original Message -----
From: "Jaffar.sidek10@xxxxxxxxx" <jaffar.sidek10@xxxxxxxxx
To: <pythonvis@xxxxxxxxxxxxx
Date sent: Thu, 15 May 2014 13:01:21 +0800
Subject: [pythonvis] Re: Yet another indenting explanation

Hi Richard.  Thanks for this.  Just a point of clarification.
following the same satement as in your email, ;do the second and third lines of the statement occupy the same indent level?
what about if the statement incapsulates a class like this:
class window:
 dif __Init__(self, ...):
display.window()
then what should be the levels of indent?
Cheers and thanks.

From: Richard Dinger
Sent: Thursday, May 15, 2014 12:21 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Yet another indenting explanation

Many years ago the computer scientist Edsger Dijkstra demonstrated that any program can be structured out of combinations of the following four basic constructs: 1 A sequence of statements: execution of the statements is sequential. 2 Branch or conditional control-statement: execution branches to a different subordinate statement sequence depending on some condition. 3 loop control-statement: execution of a subordinate sequence of statements is repeated until some condition is met. 4 function call statement: execution jumps to a subordinate statement sequence and then continues immediately after the point of the function call.

So how is a subordinate statement sequence specified in the source code? Different programming languages use different approaches. Note the term statement block may also be used. Algol languages use begin and end to delimit the sequence as in the following if-statement:

if x < 0 then
 begin
 x = abs(x)
 count = count + 1
 end

The C family of languages use braces for a more mathematical looking approach. In C languages the same if-statement is:

if (x < 0)
 {
 x = abs(x)
 count += 1
 }

And Python simply uses the indenting alone like this:

if x < 0:
 s = abs(x)
 count += 1

You must tell the computer somehow what statements are to be executed when a control-statement is executed. Python uses the indentation and nothing else. So in our example, the two statements are indented one more level from the level of the controlling if statement's level and the subordinate statement sequence continues until the indenting returns to a lower level. Note that this is laid out like an outline or a set of help files where each subsequent level is indented one more level to the right.

The rules for indenting are simple. Start at the left margin in a file or level0. Control-statements introduce subordinate statement sequences of one or more statements, which are indented to the next level. You cannot increase indentation except after a control statement such as a loop or conditional. Note all such statements end in a colon. The block of indented statements ends when the level of indentation decreases to a previous level.

If you are still uncertain, try writing some short nonsense programs where you can test indenting. Try a sequence of two print statements and then a third print one level deeper. What happens?

Other related posts: