The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by lotus]

On the first paragraph, said "But you will receive this exception because
Python maps the tab to eight spaces"

The second paragraph to the last one, said "The accepted standard is to simply
use four spaces (or a tab) for each level of indentation"

My question is in Python, tab = 4 spaces or 8 spaces?
I checked Python 1.5.2, the tab defined to 4 spaces.

Thanks
Sharon
import-bot (20211) [Avatar] Offline
#2
Re: Question on Page 95
[Originally posted by daryl harms]

> On the first paragraph, said "But you will receive this exception because
> Python maps the tab to eight spaces"
>
> The second paragraph to the last one, said "The accepted standard is to simply
> use four spaces (or a tab) for each level of indentation"
>
> My question is in Python, tab = 4 spaces or 8 spaces?
> I checked Python 1.5.2, the tab defined to 4 spaces.
>
> Thanks
> Sharon


Hi Sharon,

This is certainly confusing. If you type a tab on the first indentation level
of the Python command line mode, it will display as four spaces. However, it
will be interpreted internally by the Python parser as being eight characters.
You can see this if you open the command line mode and type in the following:

--------------------------------------------------------------------------
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> for i in [1,2]:
... print i # indented with a tab here
... print i # indented with a tab again which works
... print i # indented with 4 spaces which won't work
inconsistent dedent
File "<stdin>", line 4
print i # indented with 4 spaces which won't work
^
SyntaxError: invalid token
>>>
--------------------------------------------------------------------------

The last print statement looks to be indented the same as the previous two,
but the parser flags it as a dedent error as it sees its indentation as four
spaces which is less than the eight spaces that it sees for the two lines
which use a tab for their indentation.

Another way to see this would be to continue and try:

--------------------------------------------------------------------------
>>> for i in [1,2]:
... print i # indented with a tab here
... print i # indented with eight spaces here
...
1
1
2
2
>>>
--------------------------------------------------------------------------

Here, even though the indentation looks wrong, it works as the first line is
seen as being indented eight spaces.

I don't expect you will normally run into problems with this. If you don't mix
tabs and spaces you are always safe. If you use an environment like IDLE
(which I recommend) you should never see it. When you use its automatic
indentation (it will only enter in spaces) and when you enter the indentation
yourself, it will automatically convert any tabs you enter to four spaces and
thus that is what the Python parser will see. Emacs and most other editors can
be set to do the same.

The only times you might run into it are if you obtain some code from someone
else which is using tabs and you modify it using spaces or you use an editor
that doesn't do the conversion (and is displaying tabs as four spaces) and mix
them in your own code.

Anytime you do find yourself in the situation where you are obtaining an
indentation error even though the code looks correct. Try opening it or
copying it into IDLE or Emacs, apply their "untabify region" command and then
save. This will convert the tabs to spaces (so you see it the same as the
Python parser sees it) and should solve this problem.

I hope this clarifies things. As it says on that page, this isn't something I
expect readers will run into often (if ever), but if they do it can be really
confusing so its something I wanted to be sure they knew about.

Daryl
import-bot (20211) [Avatar] Offline
#3
Re: Question on Page 95
[Originally posted by lotus]

I am not totally understood your explaination.

When you use its automatic
> indentation (it will only enter in spaces) and when you enter the indentation
> yourself, it will automatically convert any tabs you enter to four spaces and
> thus that is what the Python parser will see. Emacs and most other editors can
> be set to do the same.

What is the Python parsers sees , " a Tab" or "four spaces" ?

>
> The only times you might run into it are if you obtain some code from someone
> else which is using tabs and you modify it using spaces or you use an editor
> that doesn't do the conversion (and is displaying tabs as four spaces) and mix
> them in your own code.

If Python parsers sees "four spaces", the an editor doesn't do the conversion
, and is displaying ta s as four spaces, why give indentation error?

> save. This will convert the tabs to spaces (so you see it the same as the
> Python parser sees it) and should solve this problem.
>
"What do you means by "convert", if it is just replace the tab with four spaces?

Thanks.
Sharon
import-bot (20211) [Avatar] Offline
#4
Re: Question on Page 95
[Originally posted by daryl harms]

> I am not totally understood your explaination.
>
> When you use its automatic
> > indentation (it will only enter in spaces) and when you enter the
indentation
> > yourself, it will automatically convert any tabs you enter to four spaces
and
> > thus that is what the Python parser will see. Emacs and most other editors
can
> > be set to do the same.
>
> What is the Python parsers sees , " a Tab" or "four spaces" ?
>
In the case above, what is being referred to is what IDLE will do when it is
set to indent at four spaces (the default). In this situation it is actually
placing four spaces into the buffer whenever you type in a tab (or more
specifically the number of spaces necessary to move to the next four space
indent level or tabstop). When the file is saved and then sent to the Python
parser it will just see the spaces as there will be no actual tab characters
stored in the file.

Most other editors can be set to do the same (and most often do it as the
default). That is, entering a number of spaces into the buffer (either a fixed
number or filling in until the next tab-stop)everytime you type in a tab.

This is done because even for text files (i.e. a text resume that is being
emailed) and languages that don't key on the indentation during parsing you
want the indentation to be at the same levels regardless of where it is
displayed. The way to ensure this is to have the stored file end up containing
only spaces and no actual tab characters.

> >
> > The only times you might run into it are if you obtain some code from
someone
> > else which is using tabs and you modify it using spaces or you use an editor
> > that doesn't do the conversion (and is displaying tabs as four spaces) and
mix
> > them in your own code.
>
> If Python parsers sees "four spaces", the an editor doesn't do the conversion
> , and is displaying ta s as four spaces, why give indentation error?
>
The problem is that the parser knows nothing about the editor that is being
used. It is configured to always interpet a tab as eight spaces. I won't
argue that the parser should do this, but eight spaces for a tab was the
original "standard".

However, for code (which often has many levels of indentation) people have
been moving towards using four space indents (which is enough for the eyes to
easily differentiate the levels and the code doesn't run off of the right end
of the screen as quickly as it will with eight space indents).

> > save. This will convert the tabs to spaces (so you see it the same as the
> > Python parser sees it) and should solve this problem.
> >
> "What do you means by "convert", if it is just replace the tab with four
spaces?
>
Yes, that is what it is, the "untabify region" command of editors (like emacs,
IDLE or vi) will convert any tabs to spaces (and this is just a matter of
replacing them with the necessary number of spaces to fill in to the tabstop).

I hope I'm helping with this and not confusing the issue more. On the plus
side (as I said previously), this shouldn't be something you normally have to
deal with. Your editor should be handling it for you.