Space Sensitive?

Martijn Faassen m.faassen at vet.uu.nl
Tue Jun 6 13:57:08 EDT 2000


Akira Kiyomiya <akira.kiyomiya at autodesk.com> wrote:
> I hear Python is space sensitive, however, "Learning Python" from O'Reilly
> and "Python Essential Reference" from New Riders do not cover this topic.

> Could someone explain this one with a nice simple example why Python is
> space sensitive?

They don't? I don't have "Learning Python", but look at the section
'Line Structure and Indentation' in chapter 2 (page 13) of the Python
Essential Reference. 

Perhaps you're confused with 'case-sensitive'? :)

Example of Python's whitespace sensitivity (it's not just spaces. Tabs
too):

if x > 2:
    print "x is greater than 2"
    print "to be exact, x is", x
else:
    print "x is smaller than or equal to 2"
    print "Too bad, you don't win the prize"

As you see, the 'print' statements here are indented (by 4 spaces in this
case, which is common. Tabs are also frequently used. Just don't mix them)
The indentation determines the block structure. This is opposed lots of
other languages. Take for instance C, C++, Java, or Perl, where they use
'{' and '}' to determine block structure, or a language like Pascal where
'begin' and 'end' are used.

Case sensitivity is another topic. It means that to the interpreter, 'foo'
is not the same word (such as variable or function name) as 'Foo', and
also not the same word as 'FOO'. It matter whether you use uppercase or
lowercase.

Python's both whitespace-sensitive and case-sensitive.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list