Re: Misuse of <tab>

Discussion in 'Python' started by John Roth, Jul 30, 2003.

  1. John Roth

    John Roth Guest

    "Gisle Vanem" <> wrote in message
    news:3f27f0e3$...
    > I'm a .py newbie and fascinated by the simplicity of formatting.
    > No need for {} as in Perl etc. But the misuse of <tab> that many
    > .py writers do makes it hard to understand how a script operates.
    >
    > E.g.
    >
    > def main():
    > terminate = 0
    > def foo():
    > line = sys.stdin.readline()
    > <tab> try:
    > bar()
    > except:
    > terminate = 1
    >
    > main()
    >
    > Now, with an editor with different tab-settings it's difficult to see

    where
    > the try statement belongs. In 'def main()' or in 'def foo()' ?
    > I'm confused, please enlighten me.


    You're quite right - mixing spaces and tabs when indenting is
    not the thing to do. In fact, it's warned against in a number of
    places. The recommended practice is to use spaces, and
    avoid tabs completely.

    I think you'll find that all the modules in the standard library
    use spaces exclusively.

    John Roth
    >
    > --gv
    >
    >
    John Roth, Jul 30, 2003
    #1
    1. Advertising

  2. John Roth

    max Guest

    > You're quite right - mixing spaces and tabs when indenting is
    > not the thing to do. In fact, it's warned against in a number of
    > places. The recommended practice is to use spaces, and
    > avoid tabs completely.
    >
    > I think you'll find that all the modules in the standard library
    > use spaces exclusively.

    The problem with spaces is that you need to do 5 times the work of a tab
    to get decent-looking indentation :).
    max, Jul 30, 2003
    #2
    1. Advertising

  3. John Roth

    John J. Lee Guest

    max <> writes:
    [...]
    > The problem with spaces is that you need to do 5 times the work of a
    > tab to get decent-looking indentation :).


    Get an editor. :)


    John
    John J. Lee, Jul 30, 2003
    #3
  4. John Roth

    Cliff Wells Guest

    On Wed, 2003-07-30 at 10:25, max wrote:

    > The problem with spaces is that you need to do 5 times the work of a tab
    > to get decent-looking indentation :).


    Or you get a decent editor that inserts 4 spaces when you hit tab and
    erases 4 spaces when you hit backspace.

    --
    Cliff Wells, Software Engineer
    Logiplex Corporation (www.logiplex.net)
    (503) 978-6726 (800) 735-0555
    Cliff Wells, Jul 30, 2003
    #4
  5. John Roth

    Asun Friere Guest

    "Keith Jones" <> wrote in message news:<>...
    > On Wed, 30 Jul 2003 17:25:05 +0000, max wrote:
    >
    > > The problem with spaces is that you need to do 5 times the work of a tab
    > > to get decent-looking indentation :).

    >
    > Not if you have a good editor.. Use vim or emacs. In vim, try :h sts (I
    > think, or :h softtabstop) ...it would suck to have to type
    > <space><space><space><space> all the time, but you don't. :)


    If you use vim remember to put 'set expandtab' (or if you wanna be
    fancy 'au BufRead,BufNewFile *.py set expandtab' ) into your .vimrc
    (or .exrc) file. This way you can use '[Ctrl]-[T]' and '[Ctrl]-[D]'
    to in- and un-dent in insert mode, as well as '>>' and '<<' in command
    mode, without fear of hard tabs being inserted. (Though the way vi(m)
    mixes tabs and spaces is consistent enough that you wouldn't usually
    get into any trouble with it.)
    Asun Friere, Jul 31, 2003
    #5
  6. John Roth

    James Graves Guest

    Asun Friere <> wrote:

    >If you use vim remember to put 'set expandtab' (or if you wanna be
    >fancy 'au BufRead,BufNewFile *.py set expandtab' ) into your .vimrc
    >(or .exrc) file. This way you can use '[Ctrl]-[T]' and '[Ctrl]-[D]'
    >to in- and un-dent in insert mode, as well as '>>' and '<<' in command
    >mode, without fear of hard tabs being inserted. (Though the way vi(m)
    >mixes tabs and spaces is consistent enough that you wouldn't usually
    >get into any trouble with it.)


    That's handy. I had known about 'set expandtab' for a while, but I
    wanted to figure out how to use it for just Python.

    And now I don't have to.

    Thanks,

    James Graves
    James Graves, Jul 31, 2003
    #6
  7. John Roth

    Asun Friere Guest

    "Keith Jones" <> wrote in message news:<>...
    > > If you use vim remember to put 'set expandtab' (or if you wanna be fancy
    > > 'au BufRead,BufNewFile *.py set expandtab' ) into your .vimrc (or .exrc)
    > > file. This way you can use '[Ctrl]-[T]' and '[Ctrl]-[D]' to in- and
    > > un-dent in insert mode, as well as '>>' and '<<' in command mode,
    > > without fear of hard tabs being inserted. (Though the way vi(m) mixes
    > > tabs and spaces is consistent enough that you wouldn't usually get into
    > > any trouble with it.)

    >
    >
    > Oh, yeah, forgot to mention expandtab; I did not know about >>, <<,
    > Ctrl+T, and Ctrl+D, however; so thanks for those. I had mapped <tab> and
    > <s-tab> to : s/^/ /<cr> and : s/^ /<cr> for command mode, which I
    > guess I won't have to use anymore.


    [ctrl]-[t] and [ctrl]-[d] also exist in vanilla vi, but are much more
    nicely implemented in vim. In vi you have to make sure you are at the
    front of the line, or else you indent will be inserted into your line
    (which is hardly ever what you would want). In vim it doesn't matter
    where in the line your cursor is, the commands will move the line as a
    unit, much like your mappings would.

    I should perhaps also mention the very useful ex versions of this
    command as well. ie '>' for a single indent, '>>' for two indents,
    '<<<' for three dedents, etc, which like ex commands generally are
    especially useful when you want to move a bunch of lines at a time.

    Thanks for that resource file listing, that is going to up a whole new
    vista of vim/python usage for me.
    Asun Friere, Aug 1, 2003
    #7
  8. John Roth

    Asun Friere Guest

    (James Graves) wrote in message news:<bgbqmu$ksv$>...

    > That's handy. I had known about 'set expandtab' for a while, but I
    > wanted to figure out how to use it for just Python.


    Note how Keith Jones, in the following post, achieves the same:

    au FileType python set et

    I wasn't aware of the 'FileType' setting, but it has the advantage of
    working with files which vim recognises as python source, but which
    don't necessarily end in 'py'. Nice!
    Asun Friere, Aug 1, 2003
    #8
  9. John Roth

    Robin Munn Guest

    Keith Jones <> wrote:
    > On Wed, 30 Jul 2003 17:25:05 +0000, max wrote:
    >
    >> The problem with spaces is that you need to do 5 times the work of a tab
    >> to get decent-looking indentation :).

    >
    > Not if you have a good editor.. Use vim or emacs. In vim, try :h sts (I
    > think, or :h softtabstop) ...it would suck to have to type
    ><space><space><space><space> all the time, but you don't. :)


    Don't forget the "smarttab" option :)h smarttab). My .vimrc looks like:

    set shiftwidth=4
    set expandtab
    set smarttab

    OK, if you want to be precise about it, *part* of my .vimrc looks like
    the above. :)

    --
    Robin Munn <> | http://www.rmunn.com/ | PGP key 0x6AFB6838
    -----------------------------+-----------------------+----------------------
    "Remember, when it comes to commercial TV, the program is not the product.
    YOU are the product, and the advertiser is the customer." - Mark W. Schumann
    Robin Munn, Aug 12, 2003
    #9
    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. Simon North
    Replies:
    0
    Views:
    350
    Simon North
    Aug 5, 2004
  2. Peter Flynn
    Replies:
    2
    Views:
    419
    Peter Flynn
    Aug 9, 2004
  3. Michael Laplante

    Page won't validate -- misuse of A element?

    Michael Laplante, May 18, 2006, in forum: HTML
    Replies:
    3
    Views:
    461
    Jonathan N. Little
    May 18, 2006
  4. Michael Sampson

    Re: Misuse of <tab>

    Michael Sampson, Jul 30, 2003, in forum: Python
    Replies:
    5
    Views:
    355
    Ben Finney
    Jul 31, 2003
  5. naive misuse?

    , Aug 28, 2006, in forum: Python
    Replies:
    3
    Views:
    333
    Simon Forman
    Aug 29, 2006
Loading...

Share This Page