[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Tab indentions on different platforms?

xkenneth

12/28/2007 10:51:00 PM

All,


I seem to be having problems with running my python code, written
on a Mac, on Linux and Windows boxes. It seems like the problem has to
do with tab indention, particularly I've noticed that my code will
completely ignore try statements. Has anyone had a similar problem?

Regards,
Ken
26 Answers

John Machin

12/28/2007 11:14:00 PM

0

On Dec 29, 9:51 am, xkenneth <xkenn...@gmail.com> wrote:
> I seem to be having problems with running my python code, written
> on a Mac, on Linux and Windows boxes.

So *what* problems do you seem to be having? More to the the point,
what problems are you *actually* having?

> It seems like the problem has to
> do with tab indention, particularly I've noticed that my code will
> completely ignore try statements. Has anyone had a similar problem?

Yes.

Set up your text editor to (a) recognise Tab and Shift-Tab keyboard
inputs as indent/dedent respectively and (b) to insert 4 spaces and
zero tab characters per indent. IOW, don't have tab characters at all
in your source files.

If after that you still have a problem, ask here again, with the
following information:

1. The shortest possible script file that reproduces the problem --
use
print open('problemdemo.py', 'rb').read()
to show the contents (a) as it appears on the Mac (b) after
transferring [how?] to Linux (c) after transferring [how?] to Windows

2. The results of executing the code in each of the three environments
-- copy/paste of the actual output, not a description.

Thorsten Kampe

12/29/2007 3:29:00 PM

0

* xkenneth (Fri, 28 Dec 2007 14:51:04 -0800 (PST))
> I seem to be having problems with running my python code, written on
> a Mac, on Linux and Windows boxes.

You seem to have problems or you do have problems?

> It seems like the problem has to do with tab indention,

Why does it seem and what does seem?

> particularly I've noticed that my code will completely ignore try
> statements.

Statements are never ignored: they error or they don't.

> Has anyone had a similar problem?

It doesn't matter how a particular editor (note, that's not an OS
thing!) displays indents. If you use only tabs or spaces that's just
fine. I'd personally go for spaces because:

1. I don't like things I cannot see (control characters)

2. I never had problems with spaces but plenty with tabs


Thorsten

Steven D'Aprano

12/30/2007 12:38:00 AM

0

On Sat, 29 Dec 2007 15:29:25 +0000, Thorsten Kampe wrote:

> I'd personally go for spaces because:
>
> 1. I don't like things I cannot see (control characters)

You can see spaces but not tabs? Your editor is pretty weird. In all the
editors I've every used, both spaces and tabs show up as empty white
space. (Or coloured space if I set the editor to use a coloured
background.)


> 2. I never had problems with spaces but plenty with tabs

Periodically, I ask on this list what problems people have with tabs.
(I'm fully aware that mixing tabs and spaces is a Bad Thing.) I've had
little luck getting any answer except "Tabs are bad, m'kay?".

I'm only aware of two problems that some people run into due to the
consistent use of tabs rather than spaces.

* Some news clients don't display tabs correctly, thus making it hard to
copy and paste code direct out of the news reader into your source;

(But of course if you copy code using spaces, and the number of space
characters used per indent level doesn't match your source code, you'll
have problems too.)

* Some applications "helpfully" convert tabs into spaces, thus meaning
that when copy code from them, you end up with spaces instead of tabs.

What problems have you had with tabs that aren't related to buggy
applications or users that mix tabs and spaces?



--
Steven

Ben Finney

12/30/2007 4:36:00 AM

0

Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> On Sat, 29 Dec 2007 15:29:25 +0000, Thorsten Kampe wrote:
>
> > I'd personally go for spaces because:
> >
> > 1. I don't like things I cannot see (control characters)
>
> You can see spaces but not tabs? Your editor is pretty weird. In all
> the editors I've every used, both spaces and tabs show up as empty
> white space. (Or coloured space if I set the editor to use a
> coloured background.)

Though Thorsten could have been clearer, "thing that is not a space
character but shows up as white space" is a near-enough approximation
of "thing I cannot see".

> > 2. I never had problems with spaces but plenty with tabs
>
> Periodically, I ask on this list what problems people have with
> tabs. (I'm fully aware that mixing tabs and spaces is a Bad Thing.)
> I've had little luck getting any answer except "Tabs are bad,
> m'kay?".

Posit: White space is most often achieved by the user inserting a
sequence of space characters (U+0020).

Posit: Tab characters (U+0009) are, in a majority of environments,
rendered visually indistinguishable from a sequence of space
characters.

Corollary: most readers will, when seeing a stretch of white space on
a line, default to assuming that it represents a sequence of space
(U+0020) characters.

Corollary: So when a file containing either spaces or tabs is edited
in such an environment, the common way chosen by the user to get to
the same indentation level as existing lines is to prepend space
characters (using the spacebar or the Tab key or whatever facility the
editor provides) until the indentation lines up visually --
remembering the caveat that tabs and space-sequences are visually
indistinguishable in many environments.

Argument: The user will get an unexpected result when they do the
obvious thing (prepend space characters) in a tabs-only file. With
existing spaces-only files, the obvious way to get matching
indentation gives the expected result.

Conclusion: Thus, using tabs-only is inferior to using spaces-only for
indentation, because it violates the Principle of Least Astonishment
<URL:http://en.wikipedia.org/wiki/Principle_of_least_astoni....

--
\ "I object to doing things that computers can do." â??Olin |
`\ Shivers |
_o__) |
Ben Finney

John Nagle

12/30/2007 4:48:00 AM

0

Thorsten Kampe wrote:
> * xkenneth (Fri, 28 Dec 2007 14:51:04 -0800 (PST))
>> I seem to be having problems with running my python code, written on
>> a Mac, on Linux and Windows boxes.
>
> You seem to have problems or you do have problems?
>
>> It seems like the problem has to do with tab indention,

It really should be a syntax error in Python to mix
leading tabs and leading spaces in the same file. One can argue over
which to use, but a file with both usually leads to trouble.

John Nagle

John Machin

12/30/2007 5:13:00 AM

0

On Dec 30, 3:48 pm, John Nagle <na...@animats.com> wrote:
> Thorsten Kampe wrote:
> > * xkenneth (Fri, 28 Dec 2007 14:51:04 -0800 (PST))
> >> I seem to be having problems with running my python code, written on
> >> a Mac, on Linux and Windows boxes.
>
> > You seem to have problems or you do have problems?
>
> >> It seems like the problem has to do with tab indention,
>
> It really should be a syntax error in Python to mix
> leading tabs and leading spaces in the same file. One can argue over
> which to use, but a file with both usually leads to trouble.
>

It can be made to do so:

prompt> python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
[snip]
-t : issue warnings about inconsistent tab usage (-tt: issue
errors)
[snip]

Further advice to the OP: run python with the -t (or -tt) option and
see what you get.

Thorsten Kampe

12/30/2007 8:33:00 PM

0

* Steven D'Aprano (Sun, 30 Dec 2007 00:37:32 -0000)
> On Sat, 29 Dec 2007 15:29:25 +0000, Thorsten Kampe wrote:
> > I'd personally go for spaces because:
> >
> > 1. I don't like things I cannot see (control characters)
>
> You can see spaces but not tabs? Your editor is pretty weird. In all the
> editors I've every used, both spaces and tabs show up as empty white
> space.

That's because the editor displays those invisible tab control
characters as something they're not: spaces.

> > 2. I never had problems with spaces but plenty with tabs
>
> What problems have you had with tabs that aren't related to buggy
> applications or users that mix tabs and spaces?

Fortunately I don't remember every incident when I stumbled about
something weird behaving or looking. One thing I do remember is
reading python source code (not my own) with a pager. The source code
was badly indented. The pager was not "buggy".

Thorsten

Thorsten Kampe

12/30/2007 8:41:00 PM

0

* Ben Finney (Sun, 30 Dec 2007 15:36:12 +1100)
> Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:
> > On Sat, 29 Dec 2007 15:29:25 +0000, Thorsten Kampe wrote:
> > > I'd personally go for spaces because:
> > >
> > > 1. I don't like things I cannot see (control characters)
> >
> > You can see spaces but not tabs? Your editor is pretty weird. In all
> > the editors I've every used, both spaces and tabs show up as empty
> > white space. (Or coloured space if I set the editor to use a
> > coloured background.)
>
> Though Thorsten could have been clearer, "thing that is not a space
> character but shows up as white space" is a near-enough approximation
> of "thing I cannot see".
>
> > > 2. I never had problems with spaces but plenty with tabs
> >
> > Periodically, I ask on this list what problems people have with
> > tabs. (I'm fully aware that mixing tabs and spaces is a Bad Thing.)
> > I've had little luck getting any answer except "Tabs are bad,
> > m'kay?".
>
> Posit: White space is most often achieved by the user inserting a
> sequence of space characters (U+0020).
>
> Posit: Tab characters (U+0009) are, in a majority of environments,
> rendered visually indistinguishable from a sequence of space
> characters.
>
> Corollary: most readers will, when seeing a stretch of white space on
> a line, default to assuming that it represents a sequence of space
> (U+0020) characters.
>
> Corollary: So when a file containing either spaces or tabs is edited
> in such an environment, the common way chosen by the user to get to
> the same indentation level as existing lines is to prepend space
> characters (using the spacebar or the Tab key or whatever facility the
> editor provides) until the indentation lines up visually --
> remembering the caveat that tabs and space-sequences are visually
> indistinguishable in many environments.
>
> Argument: The user will get an unexpected result when they do the
> obvious thing (prepend space characters) in a tabs-only file. With
> existing spaces-only files, the obvious way to get matching
> indentation gives the expected result.
>
> Conclusion: Thus, using tabs-only is inferior to using spaces-only for
> indentation, because it violates the Principle of Least Astonishment
> <URL:http://en.wikipedia.org/wiki/Principle_of_least_astoni....

Man, how did you know what I wanted to say (but failed to to express)
:-) ? Anyway: the consequence of your well done argumentation is that
someone editing Python code has to use a specialised editor to prevent
screwing up tab indented code - and that's bad.

Thorsten

Steven D'Aprano

12/30/2007 11:29:00 PM

0

On Sun, 30 Dec 2007 20:33:19 +0000, Thorsten Kampe wrote:

> * Steven D'Aprano (Sun, 30 Dec 2007 00:37:32 -0000)
>> On Sat, 29 Dec 2007 15:29:25 +0000, Thorsten Kampe wrote:
>> > I'd personally go for spaces because:
>> >
>> > 1. I don't like things I cannot see (control characters)
>>
>> You can see spaces but not tabs? Your editor is pretty weird. In all
>> the editors I've every used, both spaces and tabs show up as empty
>> white space.
>
> That's because the editor displays those invisible tab control
> characters as something they're not: spaces.

Editors display tab characters as "indent to next tab stop". That's what
they're for, and any editor that doesn't behave that way is buggy.

Tab characters are no more invisible than space characters. How can you
see space characters but not tabs? Why aren't space characters invisible
too? Why do you dislike tabs *specifically* because they are invisible,
but like invisible spaces?



>> > 2. I never had problems with spaces but plenty with tabs
>>
>> What problems have you had with tabs that aren't related to buggy
>> applications or users that mix tabs and spaces?
>
> Fortunately I don't remember every incident when I stumbled about
> something weird behaving or looking. One thing I do remember is reading
> python source code (not my own) with a pager. The source code was badly
> indented. The pager was not "buggy".

Given the constraints of the typical pager one-line display, and the
tendency of pagers I've seen to arbitrarily drop leading whitespace *of
any sort* from the start of lines, I'd like to know how you could tell
the indentation was wrong.

If the source code was correctly indented, but displayed wrongly by the
pager, then of course the pager was buggy. And if the source code was
incorrectly indented and the pager showed it as it was, well, that can
happen with spaces too:

x = 1
for i in range(20):
x += i
print x


No tabs there, all the indentation is done with spaces. How would you
expect a pager to display that?



--
Steven

Steven D'Aprano

12/31/2007 12:05:00 AM

0

On Sun, 30 Dec 2007 20:41:09 +0000, Thorsten Kampe wrote:

> Anyway: the consequence of your well done argumentation is that
> someone editing Python code has to use a specialised editor to prevent
> screwing up tab indented code - and that's bad.

You just need to use an editor that inserts tab characters when the tab
key is pressed, just like you use an editor that inserts s characters
when the s key is pressed. If people didn't insist on using spaces where
tabs are called for, there would be no problem, and you wouldn't need a
specialized editor to prevent screwing up space-indented code.



--
Steven