Fog Creek Software
Discussion Board




Python: Whitespace and OO


A recent thread was discussing Python and several comments were made that I thought were interesting, and I like to hear more comments from people who both know Python and those who don't.

A) Whitespace in Python syntax is significant and is used to indicate blocking (rather than {}). This bothered me at first too, but once our group all had the same editor settings we rarely ran afoul of it, and it did make the code look cleaner. Some posters have said that they basically rejected Python *on this basis alone*. I find this somewhat of a myopic view, but I understand it. Are there other criteria that people use as first-line filters on whether they will learn/study/use a language or not?

B) I've heard the comment before that the Python is object oriented. Python is actually namespace-oriented, and the name resolution rules work such that you get OO behavior -- polymorphism, inheritance. But fundamentally it doesn't really seem OO in the same way that, say, Java or Eiffel is. More object-based like (old) VB. I'd be curious to know what others think. If a language supports polymorphism as a run-time behavior, does that make it an OO language?

Jeff Kotula
Thursday, August 12, 2004

>> "Are there other criteria that people use as first-line filters on whether they will learn/study/use a language or not?"

The criteria I have to learn and use a new language is that it has to make me money.  C/C++/VB/ASP/HTML/Java/C#/ASP.NET/VB.NET all make me money.  If Python would be me an immediate source of income for me then yes I would pursue it further.  No money = waste of time.

Peace
Thursday, August 12, 2004

Peace -

And that is precisely why you're most likely a poor developer.  Anyone who learns to program for the sake of the income it can bring him is not properly motivated to innovate.  I'm willing to bet that you're the sort who argues fervently for methodology and convention.

muppet
Thursday, August 12, 2004

Nope.  I'm actually a very well respected developer and have built one heck of a consulting business (We take in 7 figures per year.)  I don't care too much about methodology or convention as long as the work gets done and the customer is satisfied.  If I had a use for Python that would bring in a good cash flow then I would certainly pursue it.

Peace
Thursday, August 12, 2004

The whitespace trick in python is it's single greatest innovation - it's just a pity it's poorly implemented. What they should have done is require TABS and make proceeding spaces a syntax error. That way it wouldn't be possible for differing editors to confuse it's indentation.

Python's whitespace trick was almost enough to make me learn it.

Mr Jack
Thursday, August 12, 2004

Indentation does makes the code neat. However it is a waste and annoyance to force programmers to do it, now that code reformatting and beautification tools are available.  Python makes it impossible to use such tools.

It won't stop me from using Python, but it is annoying.

NoName
Thursday, August 12, 2004

I cringed just a little when you compared python OO to vb6(?).  Unlike vb6 Python's true OO supports inheritance, polymorphism, and encapsulation.  Using namespace resolution (via dictionaries) is just an implementation.

I have ported Java to Python and Python to Java.  In both instances the translation of object hierarchies was very natural and straight forward - probably one of the easiest parts of the porting process.  So in this regard, ignoring implemenation, OO in Python is very similar to Java but with a dynamic twist.

I remember reading on some python guru's website that Python relies on the "politeness" of the developer.  That statement holds new weight with me after working a bit with Python.  Sure you can add new methods to a class at run time, but do you really want to?  It's all developer discretion in Python, whereas it's just illegal (or extremely awkward) in Java.

lumberjack
Thursday, August 12, 2004

I'll admit it. I took a look at Python, and the main thing I disliked about it was the whitespace rules. I found these an annoyance, rather than a feature.  Reminds me too much of editing makefiles, or writing Fortran 77 or something.
At that moment, I didn't have any problem at hand for which Python seemed uniquely suited, so I took a pass. There are dozens of interesting languages to try. I'll probably work around to try Python again at some point.

Rob VH
Thursday, August 12, 2004

The whitespace rule is indeed the single best thing about Python, and what makes it even better was that the language does NOT require the horrible, terrible Tab character which should be outlawed and exterminated with utmost prejudice.

Nothing worse than a character that looks like a sequence of spaces but isn't, that look like a DIFFERENT number of spaces depending on your display settings, and that you may not even be able to type without reconfiguring your editor! Any editor lets you type spaces last time I checked.

Chris Nahr
Thursday, August 12, 2004

One of the nicest things about 'modern' computer languages (ie post Fortran 77) is their flexibility in formatting -- basically one or more white-spaces, one or more tabs, CR, LF, it doesn't matter to the compiler, it's all treated as one delimiter.

This lets you be flexible in your source-code formatting.  This is considered mostly A Good Thing.

One of the MAJOR PITA factors in Make was that it used a SINGLE-TAB following a 'target' line to signal the beginning of a 'rule' line.  Not 8 spaces, not 2 spaces, not 2 tabs, but ONE TAB.  Since the person editing the file couldn't SEE 'one-tab', this led to obscure (hard to track down, hard to see, once found easy to fix) bugs.

I'm a big believer in requireing things at their proper level.  If you want a style guide (usually A Good Thing) then write a style guide and have your programmers follow it.  Don't require the LANGUAGE for heaven's sake to enforce your style guide -- that's too low a level.

Some people say always use TABS for indentation.  Some say NEVER use TABS for indentation, since if you only uses spaces the code will look the same no matter WHAT TAB setting your editor uses.  I like the no TABS position myself.

So I'm very suspicious of Python's using white-space in this way.  It seems like a return to an approach that was dropped (for very good reasons) long ago.

AllanL5
Thursday, August 12, 2004

Hmmmm I'm just contemplating the amount of suicides that a syntax error on the number of spaces would engender.

Simon Lucy
Thursday, August 12, 2004

I don't understand the ALWAYS USE ONLY TABS people, myself.  Every code editor I've ever worked on allows you to re-map the Tab key to X-number-of-spaces, and thus when you strike the tab key, it actually inserts space characters, which will be consistent across editors.  What is so difficult about this?  Why is the Tab character sacred and necessary?

muppet
Thursday, August 12, 2004

Tabs allows you to vary indentation to taste, without having to modify the source code and they indent a sensible amount with a single keystroke.

To be honest I wouldn't care if they required a specified number of spaces as an indent. It's the fact that you can use either, or a mixture of both that causes the problem and breaks the whole point of the technique anyway.

Mr Jack
Thursday, August 12, 2004

Because you might not like to look at deep indentation, but prefer one or two spaces worth.  That might not be enough for me to recognize.  My vision might be so poor that I need 10 spaces or so before I can perceive an indent.  Why should we have to agree on something like that.  The INDENT character, a.k.a. the Tab character is perfectly suited for representing indentation.

teppum
Thursday, August 12, 2004

Well I took the plunge recently and used Python instead of Java for a relatively independent part of our system. The whitespace indentation is a non-issue. Load up the python mode in your favourite IDE, hit TAB and away you go. The IDE inserts the required number of spaces and you don't even have to think about it.

It gives you the "freedom" to think about what you're
coding, not where to put the code. Coding conventions serve the same purpose in Java, C etc.

Richard Rodger
Thursday, August 12, 2004

Not to mention that in a variable spaced font the space character is usually very narrow so you need a decent number in order to see them.

Mr Jack
Thursday, August 12, 2004

The whole language == money thing IS important.

I have a friend who spent the last 5 years of his life doing Python.  Today, he cannot find work.  He seems to be a rather skilled software engineer, has a BSCS from a Simon Frasier U (Canada).  Is "mister Reliable" and from my experience a hard worker.

Cannot get a job right now, since he does not have C/C++/C#/Java experience.  Unfortunately, the language counts.  And it sucks.

hoser
Thursday, August 12, 2004

"And that is precisely why you're most likely a poor developer..."

Another ad hominem attack from muppet!  The streak continues!

muppets suck
Thursday, August 12, 2004

And thus we see the inevitable Python argument repeated.

Tabs vs. spaces is fought over at about two week intervals over on comp.lang.python. If you feel the need to argue it, please go over to google groups first; whatever you want to say is already in there. Just post a link, it'll save everyone lots of bandwidth. :-) This is the python equivalent of the C family's "where the braces go" argument, and just as productive.

Python's use of whitespace is FAR different than Fortran 77's. Fortran said "thou shalt start in column 5" or whatever for no particular reason. Python's rule is "thou shalt indent consistantly". That's it.

The reason for this has to do with readability. In a C family language, you do this:

  if( something ) {
    code;
    code;
    more code;
  }

The human uses the indentation to figure out what's in the scope of the if block, unless said human works a lot harder than us lazy types are usually wont to do. So suppose the indentation looks like this instead:

  if( something ) {
    code;
  more code;
  more code;
  }

That disconnect makes things harder to read and figure out.

In python, on the other hand, the interpreter and the human use the EXACT SAME CUE to determine scoping: the indentation. So this:

  if something:
    code
    more code
    more code

and this:

  if something:
    code
  more code
  more code

work exactly the way they look.

As for the space vs. tab issue: the rule to live by is use either tabs or spaces, just don't mix them! Pick just one or the other. Any decent editor (i.e. not notepad) handles this just fine. There are actually scripts and a warning option in the interpreter that will check this, so it's no big deal.

Those who dismiss python because of the indentation are missing out on something good.

P.S. There is one case where Python's indentation rules do cause trouble - it makes it hard to embed python code inside other text, for example inside ASP pages.

Chris Tavares
Thursday, August 12, 2004

"Not to mention that in a variable spaced font the space character is usually very narrow so you need a decent number in order to see them."

You actually read or edit code using something that displays it in variable-width fonts?  Are you one of those who uses MS Word to edit code?

NoName
Thursday, August 12, 2004

"Those who dismiss python because of the indentation are missing out on something good."

I've worked on cross-platform projects coordinating with the customer's developers offsite -- one of the first things that gets screwed up is the spacing. Sad, but true.

And, some tools ignore whitespace when comparing files (MS SourceSafe, for example, but that might just be the buried in the preferences somewhere). This defeats the "verify your changes while checking it in" stage of our process.

Any language that would require someone to ensure that EVERYONE at the company has their tools configured in a specific manner, and that EVERYONE remembers to type tabs or spaces is simply causing needless problems.

And, I would have to justify spending time and money to adress these problems. My life has enough real problems.

Gustavo
Thursday, August 12, 2004

Being "suspicious" of Python's use of whitespace is curious. After all, it is just another element of the syntax, no more or less significant to the compiler than any other bit. I don't like the overuse of () in Lisp, but I'm not suspicious of the language...

There are some cases where the whitespace indent can get you into trouble, almost all of which can be avoided by having the IDE convert tabs to spaces. (Also a good idea for living peacefully with diff tools, etc.)

But perhaps the poster was more implying that the decision to use whitespace syntactically (or actually, lexically) casts doubt on the other decisions that were used in Python's design. That's an interesting thread: how much can you imply about a language's design philosophy (and therefore its usability for a given purpose) from a few select decisions?

Jeff Kotula
Thursday, August 12, 2004

No one here is saying indentation is bad.  The debate is over how to accomplish it.

I have yet to see anyone explain why Python's indentation rule is better than using a code reformatting utility.  I definitely prefer to be able to type ten statements on a single line and have the machine automagically indent everything for me.

NoName
Thursday, August 12, 2004

"After all, it is just another element of the syntax, no more or less significant to the compiler than any other bit."

But is very significant to humans who have to read and type it.

NoName
Thursday, August 12, 2004

+++Not to mention that in a variable spaced font the space character is usually very narrow so you need a decent number in order to see them.+++

if you're using anything other than a monospaced font to view/edit code, you ought be shot.

muppet
Thursday, August 12, 2004

Well, I've just started playing with Python some, and I like it.  I was wary of Python before, because of the whitespace issue, but once I started using it, it was really natural, and didn't cause any problems.  I now like the whitespace-matters approach a lot - like Chris says, both the compiler and myself now use the same block delimiters  :).  Cuts down on excess fluff.

I'd add more, but I'd just be repeating what Chris said in his (excellent) post above.

I'm not sure if there's another factor which could make me "wary" of a language - whitespace is pretty key to the look of the code, which matters a lot.

One of the Matts
Thursday, August 12, 2004

Gustavo,

I can understand your reluctance. In my experience, in python it's just not a problem after the first couple of days. Your experience might be different. Your call.

NoName,

Any decent python editor will do exactly what you're saying. You type a statement, and the editor auto-indents it. You don't have to sit there banging on the spacebar. And once the code is in the proper shape (which it will be by default) you can run tools to change the indent level.

The python indentation rules are basically "do what anyone sane does already anyway, just get rid of the extra braces/begin/end/etc." Are you saying that when writing code in other languages you don't indent consistently?

I assume by your "write ten statements on a line and let the editor format it" comment that you write code like this:

  statement;statement;statement;statement;

In python, here's the trick: instead of typing ';', press the enter key instead. Problem solved. ;-)

Chris Tavares
Thursday, August 12, 2004

The whole "I develop Python so I can't find a job in anything that isn't" is pretty lame.

I can say, as a programmer who has often interviewed other programmers, that if I talked to somebody who has spent time programming a language like Python, Ruby, ML, or Smalltalk, as long as I was relatively sure that they did understand how a non-GC'd language with pointers works, I'd give them a go.  Plus I'd give them points for geek style.  The failure of many companies to really grasp that once you know 2 or 3 languages -- especially if they are really zen-like languages like C++ or Lisp -- that you can learn most languages really is their loss.

But it's really dumb that I'm making sure that I've got at least some experience in all of the big languages that a manager is likely to say "We're going to use *this* language because Datamation says so" at a company less enlightened than my current one.

Flamebait Sr.
Thursday, August 12, 2004

I once read a claim on JoS that Bjarne Stroustrup uses a variable width font.

MacSqueeb
Thursday, August 12, 2004

"In python, here's the trick: instead of typing ';', press the enter key instead. Problem solved"

Yeah, and when I need to add or remove something like an if statement that affects the indentation of things around it, I have the tedious task of going up and down to readjust the indentation. Other languages I just have to hit the reformat button.

NoName
Thursday, August 12, 2004

When Python asks me to type "self" as the first param name to a method, I just consider it another bit of syntax. Nothing that detracts from its OOPness; it just lets a little girder or ductwork show. Maybe there are even usability advantages for it, so you don't have to memorize Python uses "self" and Java uses "this".

Ironically though, a good thing about Python is it doesn't OOP people to death. For example, it lets people use a simple functional programming style when the normal procedural or OOP way gets too complex. Each paradigm has a sweet spot, but also some areas where it gets too hairy, and so large projects can't really rely on single-paradigm wonder languages.

I strongly like using paradigms as an ensemble, using them naturally. Some might say that's too much choice, but the real complexity comes when you're hitching a ride on one paradigm which starts to spiral in complexity for some tasks.

Tayssir John Gabbour
Thursday, August 12, 2004

"""I have the tedious task of going up and down to readjust the indentation"""

Unless your editor lets you quickly select a range of lines and indent or outdent them -- and of course pretty much any editor for Python has hotkeys for indent and outdent.  If I wanted to make a block conditional, I'd do something like:

1. type the if statement and ':'
2. hit enter
3. Hold down the shift key, and arrow down to the end of the block
4. Hit Ctrl-] or TAB, depending on which editor I was using

Let's compare to a brace-based language:

1. Type the if statement and '{'
2. Hit enter
3. Arrow down to the end of the block
4. Type a closing '}'
5. Invoke the reformat command

Not a lot of difference, here.  Really, almost any decent programmer's editor should be able to handle this.

Finally, if you absolutely can't live without closing block delimiters, and want to be able to automatically reformat code, there's a tool (I think it's called "pindent") that comes with the Python distribution that treats special comments as block endings, and can reindent code that's been messed up.

The comments look like "# end", although you can add extra info like "# end for", etc.  The tool can add these comments to code that's properly indented, or it can fix the indentation of code that has these comments.  So, if you really have an environment ruled by the whitespace-eating nanoviruses from outer space (that's a comp.lang.python joke), just make it policy to run the Python indent tools over code before transmitting it anywhere.

Phillip J. Eby
Thursday, August 12, 2004

FORTRAN had a very good reason to require the lines to be indented three spaces. Each line in FORTRAN is equvalent to a punch card and there are rules as to what can go in the first three spaces (e.g. a comment char)

(We still have people programming in FORTRAN in my office. One of our engineering applications is based on an old code base. The calculations are still done with the Fortran code and the UI is done in C/C++.)

Miles Archer
Thursday, August 12, 2004

What are the good Python IDEs or Python-aware editors that can ease the indentation pain?

T. Norman
Thursday, August 12, 2004

Python on Windows comes with IDLE, a nice shell and editor with syntax highlighting and even tooltips for currently imported functions/methods.  You can also download PythonWin, which also does a certain amount of code completion.  There are numerous other Python IDE's available, both open source and commercial, see:

http://www.python.org/cgi-bin/moinmoin/IntegratedDevelopmentEnvironments

for a list.  Also, jEdit has an okay Python mode (and a Python debugger plug-in), and the PyDev plugin for Eclipse isn't bad.  For years I just used PFE, an all-purpose programmer's editor, with no special Python support at all.  (It just used TAB to indent and shift-TAB to outdent selected lines.)

Really, basic Python support really isn't much more than easy indent/outdent, and syntax highlighting if you care about it.  And it's nice if hitting enter after typing a colon indents the next line, but you can get by with just an editor that keeps the current indent when you press enter, and types the desired number of spaces when you press TAB.

Almost any decent editor will support these things, once you get it configured right.  A decent programmer's editor should include support for configuring these things by file type, so that you only set it up once and don't break your settings for other programming languages.

Phillip J. Eby
Thursday, August 12, 2004

Friend of mine seems happy with Boa Constructor. He's paid to write Python on an ambitious project, if that means anything. I get the impression that people on his team use whatever they want though. Some definitely use Emacs.

(I did some due diligence with it, but went back to that python-mode for Emacs. But that's just the way I am...) I can't speak for the other IDEs, though I'm fine with IDLE.

Tayssir John Gabbour
Thursday, August 12, 2004

"You actually read or edit code using something that displays it in variable-width fonts?  Are you one of those who uses MS Word to edit code?"

No, I use Visual C++. In fact most good code editors are capable of working in variable spaced fonts. It takes a day or two to get used to, but after that there is no going back.

As another poster mentioned, it's what Stroustrup does - see The C++ Programming Language, 3rd Edition which is where I got the idea from.

Mr Jack
Friday, August 13, 2004

"What are the good Python IDEs or Python-aware editors that can ease the indentation pain?"

WHAT indentation pain?!? Python requires no more, or less, indentation than C++.

Oh, I've typed an if statement, guess I'll hit tab to indent the next block. Oh, I've finished that block, guess I'll hit delete to move the indentation back one.

You're not using an editor that requires you to type the indentation each time are you? I didn't think such antiques were still around.

Mr Jack
Friday, August 13, 2004

With C++ or C or Java I don't need to think about indentation. Hit Reformat and it's done.  So in C++ there is less indentation pain.

Even with the help of an editor that maintains the indent after each line, when you reach the end of a logical block you still have to carefully count back the number of indents and reestablish your position.

Having indentation is a good thing, but forcing the programmer to do it manually is a pain. Maybe it isn't a pain for you, but it is for me and others.

For Python the whitespace thing and default method parameters that remember themselves on subsequent invocations are a couple of the stinkest annoyances of the language as far as I am concerned.  However, every language has its annoyances, so this won't stop me from using Python.  Actually right now I'm considering Jython for certain routines within a Java program.

NoName
Friday, August 13, 2004

Python does not introduce any new programming concept. Instead the inventor Guido Von Rossum has tastefully taken these concepts from a whole lot of languages where they are rightly done and blended into Python.  Even the whitespace is significant is not Python's creation - it exists in languages like Haskell as well.

Python aims to be a general purpose programming language supporting atleast the following programming styles:
- object oriented
- metaclass programming
- structured programming
- scripting
It supports both programming in the small and programming in the large (ZOPE and Twisted are the large applications)

As for as getting turned off by 'white space is significant' feature here is my opinion. Just out of curiosity I keep trying to learn different programming languages. In each of these languages, whether it is Haskell, Lisp, OCaml or Ruby, there are always features that does not suit my taste.  But once I tolerate them and go further and try them out I really get a taste of that language - macros in Lisp, list comprehension and type inference in Haskell, etc.

To anyone wanting to evaluate Python, put up with the whitespace matter for a while and give it a try for other Python features. Then make a judgement about Python's suitability for your project.  Once I tasted Python, I haven't been able to stop liking it.

Regards,
Hemanth

Hemanth Sethuram
Friday, August 13, 2004


I like Pype quite a bit as an IDE for Python.

Jeff Kotula
Friday, August 13, 2004


Regarding the reformat feature of C++ editors: The big weakness of these things is that they can't handle different styles for indenting comments. So if you happen to comment your code to the right of the statements, the reformatters tend to munge it up.

I'm certainly not addicted to significant whitespace, but I was pleasantly surprised that once I got over my initial resistance, it was easy and natural. (I was "pleasantly" surprised to find I can still be surprised after so many years :)

FWIW: I've never seen or read about the behavior talked about by the previous poster where default argument values are remembered. To my understanding and experience, Python default arguments work just like C++ default args.

Jeff Kotula
Friday, August 13, 2004

He was right about default arguments getting "remembered".. it's not like C++. If you assign a new value to a variable that gets a default argument or if the default value is mutable and you change it, those changes will still be there the next time you call the function. (Disclaimer: I'm new to Python, so I am sure my recollection isn't exactly accurate, but I did read about this "remembering" in the O'Reilly book "Learning Python".)


Friday, August 13, 2004

"""If you assign a new value to a variable that gets a default argument or if the default value is mutable and you change it, those changes will still be there the next time you call the function."""

Not quite correct.  If a default value is mutable, then any changes made to it will, of course, persist.  But assignment to the argument has absolutely no effect

So, as a practical matter, one should only use immutable values (strings, numbers, tuples, or None) as default arguments. The usual pattern for mutables is to make the default value None, and then create a new mutable in the function body if that argument is None.

Personally, I rarely find that I *want* a mutable default argument, anyway.  Most of the time, mutable arguments are required arguments.  The only common exception is when you are writing a memoized recursive function or some other recursive function to populate a mutable structure.

Phillip J. Eby
Friday, August 13, 2004

Thanks Phillip. That makes sense. I only ever used immutable values for argument defaults.

Jeff Kotula
Friday, August 13, 2004

So what if *you* don't want to use a mutable default?  Unless you're developing alone, you could call somebody else's code that does that.

NoName
Friday, August 13, 2004

"""So what if *you* don't want to use a mutable default?  Unless you're developing alone, you could call somebody else's code that does that."""

Um, then their code would be broken, and I would add a test case to the test suite that proves it's broken, and fix it, and last but not least, drop by their desk to have a chat with them about how default arguments work in Python.  :)

Phillip J. Eby
Friday, August 13, 2004

Too late. They left the company a year ago.

Python provides many ways to shoot others in the foot and have them feel pain two months later.  It is a very good language for small teams with highly skilled and well-disciplined programmers, but its loosey-goosey and oddball nature makes it very dangerous in the hands of large teams or average programmers.

T. Norman
Friday, August 13, 2004

> What are the good Python IDEs or Python-aware
> editors that can ease the indentation pain?

The Zeus for Windows editor will handle Python indentation:

  http://www.zeusedit.com/lookmain.html

Zeus auto-indents any new line, using the same indentation as the line above. So you can use tabs or spaces and Zeus will make sure the indentation is always correct.

Jussi Jumppanen
Saturday, August 14, 2004

** Maybe there are even usability advantages for it, so you don't have to memorize Python uses "self" and Java uses "this". **

You don't even have to use "self."  You can use whatever variable name you want to as long as you are consistent. 

full name:
Tuesday, August 17, 2004

*  Recent Topics

*  Fog Creek Home