Can you really not see that this text
is indented? If so, your news program
is seriously broken.

> is there any solution to this problems?

Discipline. Choose a standard indent and stick to it.

You can use tabs, or spaces. If you use spaces, you can choose 4 spaces,
or 8, or any number, but whatever you choose, stick to it no matter what
editor you use. Good editors will let you use the tab key to indent with
spaces. Bad editors (e.g. Windows Notepad, and I feel your pain if you
have to use it) force you to manually insert spaces.

Especially never mix tabs and spaces in the same file. If you're editing
an existing file, you must follow whatever indent standard is already in
use.

You can also pass the -t option when launching the Python interpreter to
warn about mixed tabs and spaces.

Advertisements

Gandalf a écrit :
> every time I switch editor all the script indentation get mixed up,
> and python start giving me indentation weird errors.
> indentation also hard to follow because it invisible unlike brackets
> { }
>
> is there any solution to this problems?

Doesn't pretty much everyone use spaces and a four-position indent? I
don't think I've ever come across any half-decent Python code which
didn't follow that convention.

....
> Especially never mix tabs and spaces in the same file.

Actually, the only really likely reason he sees "mixed up" indentation
is that he has mixed TAB/space source code *and* a misconfigured[0]
editor which sets the TAB stops at anything else than every 8th
character.

[0] This is an old and tedious topic ... my view on TABs is that they
are useless iff they aren't rendered the same way everywhere. The
size 8 is hard-coded into terminals, printers and programs since
ancient times; thus anything else is wrong.

I can't speak for everyone, or even "pretty much everyone"... but I
know of several people who favor the idea of "indent with tab, align
with space." The advantage to this scheme is that anyone using a
half-sane editor can very easily change the level of indentation to
their preference, meanwhile keeping the rest of the code aligned
properly (though this may well interfere with keeping line lengths to
80 columns, or some other decided-upon number). I favor this myself
actually, though I rarely use it for Python code I write, because that
almost invariably needs to work with someone else's code who insists
on the "standard" you mentioned.

I know plenty of people who prefer a full 8-column indent, feeling that
it makes indentations (and therefore the logical blocks wich the
indentation is meant to indicate) much clearer, though most of them
are primarily C coders. Some switch to 4 for python, and some prefer
to keep 8 for pretty much everything they write.
> I don't think I've ever come across any half-decent Python code
> which didn't follow that convention.

I have. Unless one defines a lack of tabs as a criteria of
"half-decent Python code" -- which I obviously don't.
> [0] This is an old and tedious topic ...

This is very true... though clearly to anyone who hasn't encountered
it before, it is rather new.
> my view on TABs is that they are useless iff they aren't
> rendered the same way everywhere. The size 8 is hard-coded into
> terminals, printers and programs since ancient times; thus
> anything else is wrong.

This, on the other hand, is quite false -- not your opinion, perhaps,
but all of the facts you've put forth in support of it. The tab size
of nearly every tty device I've interacted with in the last 25 years
*defaulted* to 8, but is configurable using any of various terminal
control programs, such as tabs, stty, etc. (though I wouldn't know how
to do this on Windows, or if it's even possible/relevant)... The
utility of adjustable tabs is what I already stated above. I'm not
saying you should change it... just that it is very much *not*
hard-coded. In fact, most of the terminal devices I've used let you
set arbitrary tab stops at whatever column positions you like.
Occasionally useful, though not to me personally.

One thing is for sure: it's essential that whatever formatting you
decide to use, everyone touching that code needs to use the same one,
or else the result is an annoying mess. Vim (and quite probably other
editors) solves this by providing a way to set the options in the file
you're editing, which is one of many reasons why I favor it over
anything else. For example, at the top of your file:

#!/usr/bin/python
# vim:ts=4:sw=4:expandtab

Though of course, using this kind of mechanism quickly becomes gross
if everyone is using a different editor, and they all support a
similar but different mechanism for doing so.

On Sun, 19 Oct 2008 15:50:59 -0400, Derek Martin <> wrote:
> On Sun, Oct 19, 2008 at 06:05:08PM +0000, Jorgen Grahn wrote:
>> Doesn't pretty much everyone use spaces and a four-position indent?
>
> I can't speak for everyone, or even "pretty much everyone"... but I
> know of several people who favor the idea of "indent with tab, align
> with space." The advantage to this scheme is that anyone using a
> half-sane editor can very easily change the level of indentation to
> their preference, meanwhile keeping the rest of the code aligned
> properly (though this may well interfere with keeping line lengths to
> 80 columns, or some other decided-upon number).

I'm happy to say I have never seen the kind of code you mention. If I
understand you correctly, if you sent me code to look at, I would
either have to change the TAB setting of all relevant tools to
four-spaced TABs (remembering to restore them afterwards), or suffer
looking at the code eight-indented -- twice the one I'm used to.
> I favor this myself
> actually, though I rarely use it for Python code I write, because that
> almost invariably needs to work with someone else's code who insists
> on the "standard" you mentioned.

Yes; what you do makes sense in a way, but it seems very incompatible
with what most people do.

What has made me dread alternate TAB settings is not the style you
describe, but the uncontrolled one where some lines are indented with
oddly-sized TABs and others (partly) with spaces. When trying to read
such code, it's not uncommon that you have to try several different
TAB sizes before you hit the one where the code stops looking like
gibberish. And often you don't find one, because someone beat you to
it and the code now uses several different TAB settings in different
parts of the file ... At least if it's Python code, chances are good
that it refuses to load such code, or that it crashes soon.

....
>> [0] This is an old and tedious topic ...
>
> This is very true... though clearly to anyone who hasn't encountered
> it before, it is rather new.
>
>> my view on TABs is that they are useless iff they aren't
>> rendered the same way everywhere. The size 8 is hard-coded into
>> terminals, printers and programs since ancient times; thus
>> anything else is wrong.
>
> This, on the other hand, is quite false -- not your opinion, perhaps,
> but all of the facts you've put forth in support of it. The tab size
> of nearly every tty device I've interacted with in the last 25 years
> *defaulted* to 8, but is configurable using any of various terminal
> control programs, such as tabs, stty, etc. (though I wouldn't know how
> to do this on Windows, or if it's even possible/relevant)...

Ok, I stand corrected. I shouldn't have brought it up, because the
defaults are what matters. Since text files contain no metainformation
about what TAB size they use, you have no chance to reconfigure your
tools for every text file they process.

On 20 Okt, 14:24, Duncan Booth <> wrote:
>
> Those were the only two suggestions given for Python projects with tabs as
> a coding style. I don't know if the first of these has more than 1
> developer, the second lists 7 people as contributors.
>
> So it looks like real-life projects do exist, just not very many or very
> large.

I think Webware uses (or used) tabs exclusively, and that was more
than a seven person project once upon a time.

"On defaultly-configured Unix systems, and on ancient dumb terminals and
teletypes, the tradition has been for the TAB character to mean ``move to
the right until the current column is a multiple of 8.'' (As it happens,
this is how Netscape interprets TAB inside <PRE> as well.) This is also
the default in the two most popular Unix editors, Emacs and vi."

This page is a little old (2002), but it states that the standards for
OpenBSD and Linux (presumably the kernels) are 8 space indents:

Ross Ridge a écrit :
> Bruno Desthuilliers <> wrote:
>> I can't remember having seen any other "standard" so far.
>>
> Ross Ridge a écrit :
>> I've seen various indentation styles used in examples on this newsgroup.
>
> Bruno Desthuilliers <> wrote:
>> I meant: in a real-life project.
>
> So? There's no reason to assume the styles people use on this newsgroup
> don't reflect those they use in "real-life" projects.

Yes there are. I can give you two:
1/ code editor are usually configured to insert X spaces when hitting
'tab' - which is not necessarily the case for newsreaders. Guess what
happens when someone writes a code snippet directly in his newsreader ?

When in Rome, do like the Romans do.
When coding python in general, use four spaces as described in style
guideline (PEP8)
However, when the code you're editing doesn't follow the guideline,
follow the code's guideline.

If the code doesn't have a guideline (e.g. the code liberally mix up tabs
and spaces), make one and enforce it.

Thus spake the Lord: Thou shalt indent with four spaces. No more, no
less.
Four shall be the number of spaces thou shalt indent, and the number
of thy
indenting shall be four. Eight shalt thou not indent, nor either
indent thou
two, excepting that thou then proceed to four. Tabs are right out.

I Allways thought that was some of the interpretations of the ZEN of
Python, so, who can enlighten me of the origin?

Ross Ridge wrote:
> Bruno Desthuilliers <> wrote:
>> I can't remember having seen any other "standard" so far.
>>
> Ross Ridge a écrit :
>> I've seen various indentation styles used in examples on this newsgroup.
>
> Bruno Desthuilliers <> wrote:
>> I meant: in a real-life project.
>
> So? There's no reason to assume the styles people use on this newsgroup
> don't reflect those they use in "real-life" projects.

Yes there is. If I type *hypothetical* code for a post, *I* have to
type all the spaces, and I often use 2 per indent level. If I type in
IDLE, *it* adds the spaces (4 per indent) automatically.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!