Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Why indentation is use to denote block of code?

Reply
Thread Tools

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

 
 
Chris Rebert
Guest
Posts: n/a
 
      09-13-2009
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/genera...-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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-13-2009
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
 
Reply With Quote
 
 
 
 
AggieDan04
Guest
Posts: n/a
 
      09-14-2009
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.
 
Reply With Quote
 
Miles Kaufmann
Guest
Posts: n/a
 
      09-14-2009
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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-14-2009
Miles Kaufmann wrote:

> whitespace-preserving [code] tag, though; and pindent is relatively old,
> and apparently not well maintained (no support for "with" blocks)).


http://bugs.python.org/issue6912
Add 'with' block support to Tools/Scripts/pindent.py

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove overall indentation preserving reletive indentation Jesse B. Ruby 2 03-27-2010 07:23 PM
Why indentation is use to denote block of code? Peng Yu Python 9 09-14-2009 11:00 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Can use of singletons denote poor project design? John Fly C++ 11 02-25-2006 05:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57