A little disappointed so far

Graham Nicholls graham at rockcons.co.uk
Mon May 19 15:52:19 EDT 2003


Robin Munn wrote:

> Graham Nicholls <graham at rockcons.co.uk> wrote:
>> Steven Taschuk wrote:
>> 
>>> Quoth Graham Nicholls:
>>>   [...]
>>> 
>>>> My tabstops are set to 2 in vi - thats what I like - sorry that the
>>>> editor has expanded the m out to 8 .
>>> 
>>> Fwiw, the dominant convention for Python code is to indent by four
>>> spaces and eschew the tab character.
>>> 
>> Hmmm again! I like my tabs.
> 
> And as long as you don't *ever* mix tabs and spaces, you should be fine.
> But be aware that the Python interpreter considers that tabstops are set
> every eight (8) characters -- see the second paragraph of:
> 
>     http://www.python.org/doc/current/ref/indentation.html
> 
> Therefore, if you use 2-character tabstops, or any number other than 8,
> you should be aware that what you are seeing in your editor, and what
> the Python interpreter is seeing, are two very different things. This
> *will* bite you eventually if you don't pay very close attention. For
> example, let's say you downloaded some Python code and are editing it.
> 
> # Some sample Python code using 4-space indents
> for i in range(10):
>     print i, i*i
> print "All done!"
> 
> Now you decide to change this code to use a well-named temporary
> variable to store the result of the calculation. So you move the cursor
> to the "for" line and hit o to insert a new line. You then indent to the
> proper level (as it looks to you on your screen) by pressing TAB twice.
> What you see (TAB characters, normally invisible, I have represented by
>>___ below):
> 
> # Some sample Python code using 4-space indents
> for i in range(10):
>>_>_square = i*i
>     print i, square
> print "All done!"
> 
> What the Python interpreter sees:
> 
> # Some sample Python code using 4-space indents
> for i in range(10):
>>_______>_______square = i*i
>     print i, square
> print "All done!"
> 
> The result is an IndentationError when Python hits the "print i, square"
> line. But in your editor, it looks fine! This could cause you a lot of
> grief unless you understand what's happening.
> 
> There are two (well, three really, but one of them I wouldn't recommend)
> ways of dealing with this problem:
> 
> 1. Every time you want to use someone else's code, spend time changing
> the indentation to consistently use 2-character tabs everywhere. This is
> time-consuming and error-prone. Not recommended.
> 
> 2. Set your tabstops to 8. Your code will suddenly look a lot more
> spread out, but at least you won't hit unexpected IndentationErrors
> anymore.
Ugh! I can't do it.
> 
> 3. Start using space characters for indentation. You can still get the
> "feel" of using tabs with the right editor options. In my .vimrc, for
> example, I have the following options:
>     set tabstop=8
>     set shiftwidth=4
>     set expandtab
>     set smarttab
> The "smarttab" option is the key here: it lets me hit the Tab key at the
> start of a line to indent by (shiftwidth) characters. Disadvantages:
> you're not using tabs; if that's important to you, you won't like this
> method. Advantages: this actually "feels" like you're inserting Tabs,
> but space characters are actually going into the file. And pressing the
> Backspace key also dedents by the appropriate amount.
> 
I'll look at smarttab.  I reckon I know vim pretty well, but I've learnt 2
new things: smarttabs, and folding. Thats made my day! :-)

> No, I am *not* going to get into a discussion of whether tabs or spaces
> are better in source code. If you want to use tabs, go ahead. I'm just
> trying to point out what you need to know if you're going to do so.
> 
Good (and thanks!) - I want to get on with coding! I've spent hours
responding here & by email, which wasn't really what I expected - I've
never been so overwhelmed, (except when I said in the perl ng that python
was... no I'm kidding) with info, much of which has been useful
-- 
Graham Nicholls
All round good guy.




More information about the Python-list mailing list