Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   indentation (http://www.velocityreviews.com/forums/t640531-indentation.html)

Gandalf 10-19-2008 02:16 PM

indentation
 
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!

Steven D'Aprano 10-19-2008 02:34 PM

Re: indentation
 
On Sun, 19 Oct 2008 07:16:44 -0700, Gandalf wrote:

> 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



--
Steven

Bruno Desthuilliers 10-19-2008 04:48 PM

Re: indentation
 
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.

Bruno Desthuilliers 10-19-2008 05:03 PM

Re: indentation
 
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.


Jorgen Grahn 10-19-2008 06:05 PM

Re: indentation
 
On 19 Oct 2008 14:34:45 GMT, Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> wrote:
> On Sun, 19 Oct 2008 07:16:44 -0700, Gandalf wrote:
>
>> 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?

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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!

Derek Martin 10-19-2008 07:50 PM

Re: indentation
 
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 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+jdjdlQoHP510RApbJAJ9kUc7fZ7WLw19YoruvOs OzEylmjgCdHW9x
rMQ8qfjQ+DyQew5o6bN5gSw=
=qpRT
-----END PGP SIGNATURE-----


bearophileHUGS@lycos.com 10-19-2008 08:41 PM

Re: indentation
 
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

Jorgen Grahn 10-19-2008 09:08 PM

Re: indentation
 
On Sun, 19 Oct 2008 15:50:59 -0400, Derek Martin <code@pizzashack.org> 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.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!

Steven D'Aprano 10-19-2008 09:58 PM

Re: indentation
 
On Sun, 19 Oct 2008 19:03:29 +0200, Bruno Desthuilliers wrote:

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


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


--
Steven

Bruno Desthuilliers 10-20-2008 09:01 AM

Re: indentation
 
Steven D'Aprano a écrit :
> On Sun, 19 Oct 2008 19:03:29 +0200, Bruno Desthuilliers wrote:
>
>> 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.

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


All times are GMT. The time now is 08:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.