Re: Why indentation is use to denote block of code?

Discussion in 'Python' started by Chris Rebert, Sep 13, 2009.

  1. Chris Rebert

    Chris Rebert Guest

    On Sun, Sep 13, 2009 at 3:12 PM, Peng Yu <> wrote:
    > Hi,
    >
    > I want to understand why python use indentation to denote block of
    > code. What are the advantages of it?


    See the FAQ:
    http://www.python.org/doc/faq/general/#why-does-python-use-indentation-for-grouping-of-statements

    > Is there a style in python to
    > denote a block of code the same as that of C++ (with '{}')?


    No.

    > One disadvantage of using indentation to denote a block of code is
    > that the runtime to automatically indent a python code would be about
    > a few times more than the runtime to automatically indent a C++ code
    > of the same length (both are in vim).


    In fact it's pretty much impossible to automatically indent Python
    code that has had its indentation removed; it's impossible to know for
    sure where the dedents should occur.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Sep 13, 2009
    #1
    1. Advertising

  2. On Sun, 13 Sep 2009 15:15:40 -0700, Chris Rebert wrote:

    > In fact it's pretty much impossible to automatically indent Python code
    > that has had its indentation removed; it's impossible to know for sure
    > where the dedents should occur.



    Just like most other syntactic elements -- if you remove all the return
    statements from Python code, or dot operators, it's impossible to
    automatically add them back in.

    The only difference is that some (badly written?) applications mangle
    leading whitespace, but very few feel free to remove other text on a whim.

    I don't recall actually using a mail client or newsreader that removes
    leading whitespace when posting, but I've occasionally seen posts from
    others with all indentation removed, so presumably such badly-behaved
    applications do exist.


    --
    Steven
    Steven D'Aprano, Sep 14, 2009
    #2
    1. Advertising

  3. Chris Rebert

    AggieDan04 Guest

    On Sep 13, 6:27 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Sun, 13 Sep 2009 15:15:40 -0700, Chris Rebert wrote:
    > > In fact it's pretty much impossible to automatically indent Python code
    > > that has had its indentation removed; it's impossible to know for sure
    > > where the dedents should occur.

    >
    > Just like most other syntactic elements -- if you remove all the return
    > statements from Python code, or dot operators, it's impossible to
    > automatically add them back in.
    >
    > The only difference is that some (badly written?) applications mangle
    > leading whitespace, but very few feel free to remove other text on a whim..
    >
    > I don't recall actually using a mail client or newsreader that removes
    > leading whitespace when posting, but I've occasionally seen posts from
    > others with all indentation removed, so presumably such badly-behaved
    > applications do exist.


    I haven't seen it in a mail client, but it's very common in internet
    forums.
    AggieDan04, Sep 14, 2009
    #3
  4. On Sep 13, 2009, at 5:38 PM, AggieDan04 wrote:
    > On Sep 13, 6:27 pm, Steven D'Aprano wrote:
    >> On Sun, 13 Sep 2009 15:15:40 -0700, Chris Rebert wrote:
    >>> In fact it's pretty much impossible to automatically indent Python
    >>> code
    >>> that has had its indentation removed; it's impossible to know for
    >>> sure
    >>> where the dedents should occur.

    >>
    >> Just like most other syntactic elements -- if you remove all the
    >> return
    >> statements from Python code, or dot operators, it's impossible to
    >> automatically add them back in.
    >>
    >> The only difference is that some (badly written?) applications mangle
    >> leading whitespace, but very few feel free to remove other text on
    >> a whim.
    >>
    >> I don't recall actually using a mail client or newsreader that
    >> removes
    >> leading whitespace when posting, but I've occasionally seen posts
    >> from
    >> others with all indentation removed, so presumably such badly-behaved
    >> applications do exist.

    >
    > I haven't seen it in a mail client, but it's very common in internet
    > forums.


    If you regularly deal with some sort of transport that messes with
    your leading whitespace, you may find Tools/scripts/pindent.py in the
    Python source distribution useful; it adds comments that act as block
    closers to your code, and can then use those comments to restore the
    correct indentation to a mangled version. (Most forums offer some
    sort of whitespace-preserving
    Code:
     tag, though; and pindent is  
    relatively old, and apparently not well maintained (no support for  
    "with" blocks)).
    
    -Miles
    Miles Kaufmann, Sep 14, 2009
    #4
  5. Chris Rebert

    Terry Reedy Guest

    Miles Kaufmann wrote:

    > whitespace-preserving
    Code:
     tag, though; and pindent is relatively old, 
    > and apparently not well maintained (no support for "with" blocks)).[/color]
    
    http://bugs.python.org/issue6912
    Add 'with' block support to Tools/Scripts/pindent.py
    Terry Reedy, Sep 14, 2009
    #5
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  2. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  3. John Fly
    Replies:
    11
    Views:
    482
    Phlip
    Feb 25, 2006
  4. Peng Yu
    Replies:
    9
    Views:
    381
  5. Jesse B.
    Replies:
    2
    Views:
    187
    Josh Cheek
    Mar 27, 2010
Loading...

Share This Page