indendation question

K

km

Hi all,

What is the standard and recommended way of indendation one should get to use in python programming ? is it a tab or 2 spaces or 4 spaces ? i am confused. kindly enlighten

thanks,
KM
 
T

Terry Reedy

km said:
Hi all,

What is the standard and recommended way of indendation one should get to
use in python programming ? is it a tab or 2 spaces or 4 spaces ? i am
confused. kindly enlighten

The recommendation in the early numbered PEP on style is 4 spaces. This is
the standard for the standard library.

TJR
 
D

Dan Bishop

km said:
Hi all,

What is the standard and recommended way of indendation one should get to use
in python programming? is it a tab or 2 spaces or 4 spaces? i am confused.
kindly enlighten

It doesn't matter how much indentation you use, as long as you're
consistent. But it is recommended to never use tabs, and *strongly*
recommended to never mix tabs and spaces.

I usually use 3 spaces. Most of the regulars on this newsgroup use 4.
 
S

Stephen Ferg

The official recommendation is to use 4 spaces, but...

.... Using tabs seems much more natural to me.

One level of indentation == one tab character.
What could be more natural?

So I always use tabs, and that's what I recommend.
 
R

Robin Munn

Stephen Ferg said:
The official recommendation is to use 4 spaces, but...

... Using tabs seems much more natural to me.

One level of indentation == one tab character.
What could be more natural?

So I always use tabs, and that's what I recommend.

The problem with tab characters is that it makes it very difficult to
share your code with others. Everyone has different settings for their
tab stops: some use 4, most use 8, a few heretics :) use 3... Thus,
your code, that you carefully arranged to be less than 80 characters
wide, will someday be read by someone who has 8-space tabs (while you
use 4-space tabs), and on his screen, it will wrap around most
annoyingly.

And if someone else is *editing* the same file as you, it will make
matters even worse, because they're likely to be using spaces for
indentation. So you've indented something with one tab, which on their
screen looks like 8 spaces. They add a line at what they believe to be
the same level of indentation: 8 spaces. Then you look at the file and
see their line as being one step more indented than yours. Now try to
guess what Python will do with that file.

This isn't just a theoretical problem: I've personally cleaned up files
(although they weren't written in Python) that had had exactly this
happen to them. That's why I recommend space characters, not tabs. Yes,
it may feel more natural to just hit the Tab key to indent -- but any
decent programming editor can be set up to insert the right number of
spaces when you hit the Tab key! In vim, for instance, look at the
"shiftwidth" and "smarttab" options.

On the other hand, read the rationale for PEP 666:

http://www.python.org/peps/pep-0666.html
 
J

Jarek Zgoda

Robin Munn said:
And if someone else is *editing* the same file as you, it will make
matters even worse, because they're likely to be using spaces for
indentation. So you've indented something with one tab, which on their
screen looks like 8 spaces. They add a line at what they believe to be
the same level of indentation: 8 spaces. Then you look at the file and
see their line as being one step more indented than yours. Now try to
guess what Python will do with that file.

That's why I have set all my editors (UltraEdit, Vim, Kate) to convert
tabs to spaces (1:4) on opening any file. Sometimes this leads to
intendation errors on startup, but having only spaces in files this
error is easy to trace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,939
Members
47,484
Latest member
JackRichard

Latest Threads

Top