The Artima Developer Community
Sponsored Link

Weblogs Forum
Myths About Indentation in Python

21 replies on 2 pages. Most recent reply: Oct 28, 2009 3:10 PM by Jame Krall

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 21 replies on 2 pages [ 1 2 | » ]
Guido van van Rossum

Posts: 359
Nickname: guido
Registered: Apr, 2003

Myths About Indentation in Python (View in Weblogs)
Posted: Sep 26, 2007 9:43 AM
Reply to this message Reply
Summary
I stumbled upon a particularly lucid explanation of Python's significant indentation, by Oliver Fromme of secnetics GmbH.
Advertisement

This is the place where to point your friends when they gripe about Python's "significant whitespace". I could't have said it better myself. There even are details on how it's parsed, for those who are interested.

http://www.secnetix.de/~olli/Python/block_indentation.hawk


Tim Keating

Posts: 1
Nickname: mrtact
Registered: Sep, 2007

Re: Myths About Indentation in Python Posted: Sep 26, 2007 10:43 AM
Reply to this message Reply
Obviously, I agree: http://www.mrtact.com/blog/2007/01/python-and-whitespace.html. Although I went so far as to argue that lacking significant leading whitespace is a language bug.

Tim Keating

Logan Hanks

Posts: 4
Nickname: loganhanks
Registered: Mar, 2005

Re: Myths About Indentation in Python Posted: Sep 26, 2007 11:49 AM
Reply to this message Reply
My only complaints about significant whitespace in Python have been the lack of choice (e.g., Haskell, which lets you choose to use braces, etc.) and the pain of multi-line comments. If compound statements could be delimited by some way other than the absence of whitespace, wouldn't anonymous functions be more feasible? And why should disabling a block of code when debugging require careful indentation?

Otherwise, it's pretty awesome, and I find myself irritated with the redundant punctuation of certain other languages.

Kumar McMillan

Posts: 1
Nickname: kumar303
Registered: Sep, 2007

Re: Myths About Indentation in Python Posted: Sep 26, 2007 12:00 PM
Reply to this message Reply
ah, good find. Two comments:

I occasionally have to maintain a legacy suite of perl modules that were coded with a misconfigured emacs mode. It was set to do lazy formatting (within emacs itself) so some of the formatting is completely whacked in any other editor! I don't drink near enough coffee to debug code like this:

      if ($file =~ /MTD/ || $file =~ /Summary/) {
if ($dow < 2) # before tuesday: wednesday of this week
{
my $dd = 3 - $dow;
$fd = DateCalc($fd,"+ $dd days");
}
else # else wednesday of next week
{
my $dd = 3 + (7-$dow);
$fd = DateCalc($fd,"+ $dd days");
}
} else {
if ($dow < 3) {
my $dd = 3 - $dow;
$fd = DateCalc($fd,"+ $dd days");
} elsif ($dow > 3) {
my $dd = 3 + (7-$dow);
$fd = DateCalc($fd,"+ $dd days");
}
}
}


Yarrrrgh!

2nd comment: if you are new to Python and have the urge to write code like this:

def my_snazzy_function():
import sys
print sys.modules
print sys.path
# END my_snazzy_function


then stop now with the # END ! I know it will be like withdrawl from a bad drug but you have to do it cold turkey, it's the only way.

Shalabh Chaturvedi

Posts: 5
Nickname: shalabh
Registered: Jun, 2004

Re: Myths About Indentation in Python Posted: Sep 26, 2007 6:26 PM
Reply to this message Reply
Although initially ambivalent about the use of indentation, I now am strongly pro-indentation (for block delimiting). In fact I'd say other languages should adopt it!

We will perhaps eventually be writing only small modules which are identified by name as they are used to build larger ones, so that devices like indentation, rather than delimiters, might become feasible for expressing local structure in the source language. –Donald E. Knuth, “Structured Programming with goto Statements”, Computing Surveys, Vol 6 No 4, Dec. 1974

After Python, whenever I use languages that use delimiters, it seems I'm doing extra work to appease the machine. Not to mention, decreasing readability and increasing the chances of human error. Delimiters are machine friendly, but indentation is user-friendly. Come to think of it, I never use delimiters for blocks in anything meant strictly for human consumption, but I do use indentation.

Nicolas Fleury

Posts: 7
Nickname: nidoizo
Registered: Dec, 2004

Re: Myths About Indentation in Python Posted: Sep 26, 2007 8:43 PM
Reply to this message Reply
I like the way Python handles indentation, but from my experience my main problem with it is that it can produce silent errors with source control merges. Also, -tt is not by default on most installations.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Myths About Indentation in Python Posted: Sep 27, 2007 6:07 AM
Reply to this message Reply
> I like the way Python handles indentation, but from my
> experience my main problem with it is that it can produce
> silent errors with source control merges.

Is the root cause that the indentation is not consistent in the source changes? For example, one uses 2 spaces and other other uses 4?

Elizabeth Wiethoff

Posts: 89
Nickname: ewiethoff
Registered: Mar, 2005

Re: Myths About Indentation in Python Posted: Sep 27, 2007 7:12 AM
Reply to this message Reply
Thank you for the link to Mr. Fromme's essay. Upon poking around a bit at his site, I found some handy Unix shell scripts as well :-)

But more importantly, thank you, Guido, for inventing Python and deciding that it should not use special characters/keywords for delimiting blocks. As I mentioned at the "What Matters About Python?" (http://www.artima.com/weblogs/viewpost.jsp?thread=41682) forum last month, I use huge fonts because of my terrible eyesight, and love the fact that Python code blocks don't waste vertical (or horizontal) real estate with } or end.

Kay Schluehr

Posts: 302
Nickname: schluehk
Registered: Jan, 2005

Re: Myths About Indentation in Python Posted: Sep 27, 2007 12:10 PM
Reply to this message Reply
> If compound statements could be delimited by
> some way other than the absence of whitespace, wouldn't
> anonymous functions be more feasible?

The dual question is more interesting for me: can anonymous whitespace delimited functions be embedded in Python code without changing the grammar a lot but only the tokenizer? I'd approach this by calling the tokenizer recursively on some pattern. Lets assume the tokenizer hits some left paren "(" and a successive "def", "while" etc. i.e. a keyword that denotates a compound statement. One has to suspend tokenizing an expression but tokenizes a statement instead. The tokenizer for the statement is spawned with the value of the current left-paren counter and it will be terminated when the counter is set to 0 by finding complementary right parens. The token stream produced by the inner tokenizer will then contain NEWLINE, INDENT and DEDENT data that can be further processed by Pythons LL1 parser.

Roland Pibinger

Posts: 93
Nickname: rp123
Registered: Jan, 2006

Re: Myths About Indentation in Python Posted: Sep 27, 2007 2:39 PM
Reply to this message Reply
The article describes no "Myths" but traps and pitfalls of Python indentation rules. It confirms that those indentation rules are a weak point of the Python language.

Jaime Wyant

Posts: 1
Nickname: jaimewyant
Registered: Sep, 2007

Re: Myths About Indentation in Python Posted: Sep 28, 2007 10:18 AM
Reply to this message Reply
How in the world can whitespace which enforces uniformity be labeled a `trap`? I dare say every (at the very least 99%) of python programmers find the indentation rules to be a strength, not a weakness.

When I first came over, I hated the forced indentation. But after using it, and more importantly *reading* other people's code, I found it a joy that all python code looks alike, to a degree.

But if you don't like it, then don't use it. But, please don't call it a pitfall or trap. Especially if you don't explain why.

jw

Kay Schluehr

Posts: 302
Nickname: schluehk
Registered: Jan, 2005

Re: Myths About Indentation in Python Posted: Sep 28, 2007 10:52 AM
Reply to this message Reply
> I dare say every (at the very least
> 99%) of python programmers find the indentation rules to
> be a strength, not a weakness.

Yes and 99% of non-Python programmers consider it as a penalty. I guess this is an invariant and won't change over time, no matter how many programmers use Python.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Myths About Indentation in Python Posted: Sep 28, 2007 10:59 AM
Reply to this message Reply
> How in the world can whitespace which enforces uniformity
> be labeled a `trap`?

To be fair, the one about mixing spaces and tabs could be considered a pitfall but I think most developers learn to avoid this anyway.

And one thing about the article that I'll mention on second reading is that "Whitespace is significant in Python source code." is not a myth. It's a fact. It's also a fact in most common programming languages.

for i in range(0,5)

is not the same as:

foriinrange(0,5)

But I actually think this strengthens the argument for python's indentation. We already use meaningful whitespace all the time. Why not take it to it's logical conclusion?

I recently built a file format where I stole the significant indentation thing. I added a check that fails the parsing if a tab is detected in the indentation. I think if Python adopted that, there would be very little to complain about. I wonder if it would make sense to go even further and require a specific number of spaces. That would probably be hard to sell I guess.

Anyone that thinks restricting how you can indent is taking away their creativity has seriously misdirected their creativity.

Roland Pibinger

Posts: 93
Nickname: rp123
Registered: Jan, 2006

Re: Myths About Indentation in Python Posted: Sep 28, 2007 12:10 PM
Reply to this message Reply
> How in the world can whitespace which enforces uniformity
> be labeled a `trap`?

Just look at the answers to the traps and pitfalls (mislabeled as "myths"):

"No, not in general ..."
"Yes and no ..."
"That's right, and you don't want that ..."

Nicolas Fleury

Posts: 7
Nickname: nidoizo
Registered: Dec, 2004

Re: Myths About Indentation in Python Posted: Sep 28, 2007 6:45 PM
Reply to this message Reply
> > I like the way Python handles indentation, but from my
> > experience my main problem with it is that it can
> produce
> > silent errors with source control merges.
>
> Is the root cause that the indentation is not consistent
> in the source changes? For example, one uses 2 spaces and
> other other uses 4?

No, not at all. If two programmers modify the same code file at the same time and one indents such code, you can end up with new code merged at the wrong indentation. It implies the merge is considering whitespace insignificant, but it is often the case when working with source control systems made or used for languages like C++, C# or Java (and Python is so good for hybrid systems). Fortunalety, most of the time such errors are not silent and unit tests can help find them, but it's still a case where Python indentation has a drawback.

Flat View: This topic has 21 replies on 2 pages [ 1  2 | » ]
Topic: Put a Flex UI On Your Application Previous Topic   Next Topic Topic: Solution Based Modelling - Lost OO Wisdom

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use