indentation

Discussion in 'Python' started by Gandalf, Oct 19, 2008.

  1. Gandalf

    Gandalf Guest

    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!
    Gandalf, Oct 19, 2008
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Oct 19, 2008
    #2
    1. Advertising

  3. 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, Oct 19, 2008
    #3
  4. 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.
    Bruno Desthuilliers, Oct 19, 2008
    #4
  5. Gandalf

    Jorgen Grahn Guest

    On 19 Oct 2008 14:34:45 GMT, Steven D'Aprano <> 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!
    Jorgen Grahn, Oct 19, 2008
    #5
  6. Gandalf

    Derek Martin Guest

    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+jdjdlQoHP510RApbJAJ9kUc7fZ7WLw19YoruvOsOzEylmjgCdHW9x
    rMQ8qfjQ+DyQew5o6bN5gSw=
    =qpRT
    -----END PGP SIGNATURE-----
    Derek Martin, Oct 19, 2008
    #6
  7. Gandalf

    Guest

    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
    , Oct 19, 2008
    #7
  8. Gandalf

    Jorgen Grahn Guest

    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.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
    Jorgen Grahn, Oct 19, 2008
    #8
  9. 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
    Steven D'Aprano, Oct 19, 2008
    #9
  10. 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.
    Bruno Desthuilliers, Oct 20, 2008
    #10
  11. Gandalf

    Ross Ridge Guest

    Bruno Desthuilliers <> wrote:
    >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

    --
    l/ // Ross Ridge -- The Great HTMU
    [oo][oo]
    -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
    db //
    Ross Ridge, Oct 20, 2008
    #11
  12. Ross Ridge a écrit :
    > Bruno Desthuilliers <> wrote:
    >> I can't remember having seen any other "standard" so far.

    >
    > I've seen various indentation styles used in examples on this newsgroup.


    I meant: in a real-life project.
    Bruno Desthuilliers, Oct 20, 2008
    #12
  13. Gandalf

    Ross Ridge Guest

    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.

    Ross Ridge

    --
    l/ // Ross Ridge -- The Great HTMU
    [oo][oo]
    -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
    db //
    Ross Ridge, Oct 20, 2008
    #13
  14. Gandalf

    Paul Boddie Guest

    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.

    Paul
    Paul Boddie, Oct 20, 2008
    #14
  15. On Mon, 20 Oct 2008 11:01:19 +0200, Bruno Desthuilliers wrote:

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



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



    --
    Steven
    Steven D'Aprano, Oct 20, 2008
    #15
  16. 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 ?

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

    !-)
    Bruno Desthuilliers, Oct 20, 2008
    #16
  17. Duncan Booth a écrit :
    > Bruno Desthuilliers <> wrote:
    >
    >> Ross Ridge a écrit :
    >>> Bruno Desthuilliers <> wrote:
    >>>> I can't remember having seen any other "standard" so far.
    >>> I've seen various indentation styles used in examples on this newsgroup.

    >> I meant: in a real-life project.
    >>
    >>

    (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.
    Bruno Desthuilliers, Oct 20, 2008
    #17
  18. Gandalf

    Lie Ryan Guest

    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?
    >
    >
    >
    > 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.
    Lie Ryan, Oct 20, 2008
    #18
  19. Gandalf

    GHUM Guest

    > 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
    GHUM, Oct 20, 2008
    #19
  20. Gandalf

    Terry Reedy Guest

    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.
    Terry Reedy, Oct 20, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SJ

    DataList Indentation

    SJ, Nov 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,056
    Patrick.O.Ige
    Nov 4, 2005
  2. jacov

    [Fwd: Indentation]

    jacov, Jul 22, 2004, in forum: Java
    Replies:
    0
    Views:
    331
    jacov
    Jul 22, 2004
  3. Daan

    Unwanted indentation

    Daan, Dec 9, 2003, in forum: HTML
    Replies:
    2
    Views:
    450
  4. Jay

    List Indentation

    Jay, Apr 20, 2004, in forum: HTML
    Replies:
    5
    Views:
    537
  5. Jesse B.
    Replies:
    2
    Views:
    190
    Josh Cheek
    Mar 27, 2010
Loading...

Share This Page