Newbie : checking semantics

Andrew Dalke dalke at dalkescientific.com
Mon May 16 15:32:05 EDT 2005


Stefan Nobis wrote:
> From time to time I teach some programming (in an institution
> called "Volkshochschule" here in Germany -- inexpensive courses
> for adults). My Python course is for absolute beginners with no
> previous programming experience of any kind.

I also taught a beginning programming course.   I found that
the hard thing was learning the concept of an algorithm.  I
don't recall people getting confused over the indentation part
that much.

Python's block indentation is derived from ABC.  ABC did various
human factors studies to test how to make a language easier to
use by beginning programmers.  Combining that with my own experience
both using and teaching the language make me distrustful of anyone
else's evidence based only on anecdotal accounts.

Leaving out the easy of learning aspect, there are two other
reasons for the indentation:
  http://python.fyxm.net/doc/essays/foreword.html

] Perhaps Python's most controversial feature is its use of indentation
] for statement grouping, which derives directly from ABC. It is one of
] the language's features that is dearest to my heart. It makes Python
] code more readable in two ways. First, the use of indentation reduces
] visual clutter and makes programs shorter, thus reducing the attention
] span needed to take in a basic unit of code. Second, it allows the
] programmer less freedom in formatting, thereby enabling a more uniform
] style, which makes it easier to read someone else's code. 


> So I would appreciate optional statements to end a block
> (indentation rules may be mandatory). This comes also very handy
> in something like Python Server Pages of mod_python (where a
> comment line to explicitly end a block is sometimes needed).

See the program "pindent.py" in the Python distribution under
Tools/scripts

# This file contains a class and a main program that perform three
# related (though complimentary) formatting operations on Python
# programs.  When called as "pindent -c", it takes a valid Python
# program as input and outputs a version augmented with block-closing
# comments.  When called as "pindent -d", it assumes its input is a
# Python program with block-closing comments and outputs a commentless
# version.   When called as "pindent -r" it assumes its input is a
# Python program with block-closing comments but with its indentation
# messed up, and outputs a properly indented version.

# A "block-closing comment" is a comment of the form '# end <keyword>'
# where <keyword> is the keyword that opened the block.  If the
# opening keyword is 'def' or 'class', the function or class name may
# be repeated in the block-closing comment as well.  Here is an
# example of a program fully augmented with block-closing comments:

# def foobar(a, b):
#    if a == b:
#        a = a+1
#    elif a < b:
#        b = b-1
#        if b > a: a = a-1
#        # end if
#    else:
#        print 'oops!'
#    # end if
# # end def foobar

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list