indentation

G

Gandalf

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?



thank you!
 
S

Steven D'Aprano

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 { }

Indentation is not invisible.

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.

See also the standard module tabnanny:

http://effbot.org/librarybook/tabnanny.htm
 
B

Bruno Desthuilliers

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?

Properly configure your eidtors to use 4 spaces (not tabs) for intentation.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :

(snip)
You can use tabs, or spaces. If you use spaces, you can choose 4 spaces,
or 8, or any number,

By all means, make it 4 spaces - that's the standard.
 
J

Jorgen Grahn

Discipline. Choose a standard indent and stick to it.

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.

If I was him, I'd check and fix my editors first, and/or tell my
coworkers to stop emitting broken[0] TABs.

/Jorgen

[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.
 
D

Derek Martin

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 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.



--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFI+4+jdjdlQoHP510RApbJAJ9kUc7fZ7WLw19YoruvOsOzEylmjgCdHW9x
rMQ8qfjQ+DyQew5o6bN5gSw=
=qpRT
-----END PGP SIGNATURE-----
 
B

bearophileHUGS

Derek Martin:
I know of several people who favor the idea of "indent with tab, align
with space." [...] I favor this myself actually, [...]

Thanks Guido, in Python3 this is finally a Syntax Error (I have asked
for this probably about three years ago).

Unfortunately the new Python-syntax-based Delight language starts with
a half-bad foot regarding indents:
http://delight.sourceforge.net/syntax.html

Bye,
bearophile
 
J

Jorgen Grahn

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.

/Jorgen
 
S

Steven D'Aprano

Steven D'Aprano a écrit :

(snip)


By all means, make it 4 spaces - that's the standard.

It's *a* standard. I believe it is the standard for the Python standard
library, but there are other standards.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :
It's *a* standard. I believe it is the standard for the Python standard
library, but there are other standards.

I can't remember having seen any other "standard" so far.
 
R

Ross Ridge

Bruno Desthuilliers said:
I can't remember having seen any other "standard" so far.

I've seen various indentation styles used in examples on this newsgroup.
No one bothers to complain when I indent using tabs, or someone else
uses 2 space indentation.

Ross Ridge
 
R

Ross Ridge

Bruno Desthuilliers said:
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 said:
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.

Ross Ridge
 
P

Paul Boddie

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.

Paul
 
S

Steven D'Aprano

Steven D'Aprano a écrit :

I can't remember having seen any other "standard" so far.


How about PEP 8? It's not even hidden deep in the bowels of the PEP --
it's almost at the top.
http://www.python.org/dev/peps/pep-0008/

"For really old code that you don't want to mess up, you can continue to
use 8-space tabs."


Then there's string.expandtabs():

expandtabs(...)
S.expandtabs([tabsize]) -> string

Return a copy of S where all tab characters are expanded using spaces.
If tabsize is not given, a tab size of 8 characters is assumed.


Here's Jamie Zawinski:
http://www.jwz.org/doc/tabs-vs-spaces.html

"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:

http://xarg.net/writing/tabs

Here's a style guide that recommends 2, 3 or 4 space indents:

http://www.cs.bris.ac.uk/Teaching/Resources/COMS12100/style/


And of course, whenever there's a difference of opinion, we can turn to
the ultimate source of all knowledge: Googlefight! *wink*

http://www.googlefight.com/index.php?lang=en_GB&word1=tab+8
+spaces&word2=tab+4+spaces

Nearly 50 million hits for "tab 8 spaces" versus a piddly 762 thousand
hits for "tab 4 spaces".
 
B

Bruno Desthuilliers

Ross Ridge a écrit :
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 ?

2/ I sometimes use 2-spaces when posting here to avoid linewraps

!-)
 
B

Bruno Desthuilliers

Duncan Booth a écrit :(snip two examples)

So it looks like real-life projects do exist,

Possibly. I just said that *I* didn't remember having seen such a thing.
 
L

Lie Ryan

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?



thank you!

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.
 
G

GHUM

I can't remember having seen any other "standard" so far.

there is this meme flowing around:

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?

Harald
 
T

Terry Reedy

Ross said:
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.
 

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
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top