Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: An interesting beginner question: why we need colon at all inthe python language?

Reply
Thread Tools

Re: An interesting beginner question: why we need colon at all inthe python language?

 
 
Thomas Jollans
Guest
Posts: n/a
 
      07-11-2011
On 07/11/2011 03:51 PM, Anthony Kong wrote:
> Hi, all,
>
> Lately I am giving some presentations to my colleagues about the python
> language. A new internal project is coming up which will require the use
> of python.
>
> One of my colleague asked an interesting:
>
> /If Python use indentation to denote scope, why it still needs
> semi-colon at the end of function declaration and for/while/if loop?/
>
> My immediate response is: it allows us to fit statements into one line.
> e.g. if a == 1: print a
>
> However I do not find it to be a particularly strong argument. I think
> PEP8 does not recommend this kind of coding style anyway, so one-liner
> should not be used in the first place!


Basically, it looks better, and is more readable. A colon, in English
like in Python, means that something follows that is related to what was
before the colon. So the colon makes it abundantly clear to the human
reader that a block follows, and that that block is to be considered in
relation to what was just said, before the colon.

Coincidentally, Guido wrote this blog post just last week, without which
I'd be just as much at a loss as you:

http://python-history.blogspot.com/2...and-colon.html

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      07-11-2011
On 2011-07-11, Thomas Jollans <(E-Mail Removed)> wrote:
> On 07/11/2011 03:51 PM, Anthony Kong wrote:
>> Hi, all,
>>
>> Lately I am giving some presentations to my colleagues about the python
>> language. A new internal project is coming up which will require the use
>> of python.
>>
>> One of my colleague asked an interesting:
>>
>> /If Python use indentation to denote scope, why it still needs
>> semi-colon at the end of function declaration and for/while/if loop?/
>>
>> My immediate response is: it allows us to fit statements into one line.
>> e.g. if a == 1: print a
>>
>> However I do not find it to be a particularly strong argument. I think
>> PEP8 does not recommend this kind of coding style anyway, so one-liner
>> should not be used in the first place!

>
> Basically, it looks better, and is more readable.


And it makes adding a "python mode" to a programming editor almost
trivial.

--
Grant Edwards grant.b.edwards Yow! I am a jelly donut.
at I am a jelly donut.
gmail.com
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      07-13-2011
Thomas Jollans <(E-Mail Removed)> wrote:
> Coincidentally, Guido wrote this blog post just last week, without which
> I'd be just as much at a loss as you:
>
> http://python-history.blogspot.com/2...indentation-an...


It's also part of the Python FAQ:

http://docs.python.org/faq/design.ht...ass-statements
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      07-13-2011
* Thomas Jollans (Mon, 11 Jul 2011 16:16:17 +0200)
> Basically, it looks better, and is more readable.


People tend to overlook the colon for the same reason they tend to
forget to set the colon in the first place:
a) it's a very weak marker in comparison to indentation and
b) it looks like doubling the markup to them (colon plus indentation)

What makes the if syntax for me even more weird, is the fact that you
can have an else clause with an else without a then clause with a then.

if x > 5:
print whatever
else:
print whatever

in comparison to:

if x > 5
then
print whatever
else
print whatever

> A colon, in English like in Python, means that something follows that
> is related to what was before the colon. So the colon makes it
> abundantly clear to the human reader that a block follows,


The block that follows makes it abundantly clear to the human reader
that a block follows.

> and that that block is to be considered in relation to what was just
> said, before the colon.


The indentation makes it abundantly clear to the human reader that that
indented block is to be considered in relation to what was just said,
before the indentation.

Thorsten
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-13-2011
Thorsten Kampe wrote:

> * Thomas Jollans (Mon, 11 Jul 2011 16:16:17 +0200)
>> Basically, it looks better, and is more readable.

>
> People tend to overlook the colon for the same reason they tend to
> forget to set the colon in the first place:
> a) it's a very weak marker in comparison to indentation and
> b) it looks like doubling the markup to them (colon plus indentation)


I can't speak for others, but speaking for myself, I wonder whether this is
a difference between English speakers and non-English speakers? To me, as a
native English speaker, leaving the colon out of a header line, as follows
below, just looks wrong.

Nobody expects the Spanish Inquisition! <OMINOUS MUSIC>
Our three weapons are
* fear
* surprise
* and ruthless efficiency
* and an almost fanatical devotion to the Pope!


Although the bullet list is indented, the header line "Our three weapons
are" looks like something is missing, as if I had started to write
something and forgotten to finish. It needs a colon to be complete:

Nobody expects the Spanish Inquisition! <JARRING CHORDS>
Amongst our weapons are:
* fear
* surprise
* ruthless efficiency
* an almost fanatical devotion to the Pope
* and nice red uniforms


The colon indicates that the sentence has more to follow: I think of it as a
pointer. It doesn't finish the thought, like a full stop, nor is it a mere
pause, like a comma or semi-colon.

An indented block on its own is surprising. It just hangs there,
with no connection to what was going on before. Why is it indented?
Is it connected to the previous sentence?

On the other hand, a colon gives the reader that connection:

It gives the reader a clue to expect additional information,
that the indented block that follows is not an independent
block, floating in space for its own reasons, but is intimately
linked to the previous line.

I know that Python is not English, and not all English conventions apply.
For example, Python uses newlines to end "sentences" (lines of code)
instead of a full stop. Nevertheless, as an English reader, it would look
wrong and awkward to drop the colon.


>> A colon, in English like in Python, means that something follows that
>> is related to what was before the colon. So the colon makes it
>> abundantly clear to the human reader that a block follows,

>
> The block that follows makes it abundantly clear to the human reader
> that a block follows.


But it's too late by then. You have to mentally backtrack.

blah blah blah blah <statement is complete>
indented block <surprise the previous line wasn't complete>


blah blah blah blah colon <statement is not complete>
indented block


--
Steven

 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      07-13-2011
* Steven D'Aprano (Wed, 13 Jul 2011 21:07:17 +1000)
> Thorsten Kampe wrote:
> > * Thomas Jollans (Mon, 11 Jul 2011 16:16:17 +0200)
> >> Basically, it looks better, and is more readable.

> >
> > People tend to overlook the colon for the same reason they tend to
> > forget to set the colon in the first place:
> > a) it's a very weak marker in comparison to indentation and
> > b) it looks like doubling the markup to them (colon plus indentation)

>
> I can't speak for others, but speaking for myself, I wonder whether this is
> a difference between English speakers and non-English speakers?


It's not a difference between English and non-English speakers but the
difference between a branch (if-then-else) and an enumeration (your
example).

> To me, as a native English speaker, leaving the colon out of a header
> line, as follows below, just looks wrong.
> [enumeration]
>
> Although the bullet list is indented, the header line "Our three weapons
> are" looks like something is missing, as if I had started to write
> something and forgotten to finish. It needs a colon to be complete:


Sure, because it's an enumeration - and not a branch or loop.

> An indented block on its own is surprising. It just hangs there,
> with no connection to what was going on before. Why is it indented?
> Is it connected to the previous sentence?


In normal text: sure. You cannot "just indent" in Python as you like.
Indentation always shows the connection.

> >> A colon, in English like in Python, means that something follows
> >> that is related to what was before the colon. So the colon makes it
> >> abundantly clear to the human reader that a block follows,

> >
> > The block that follows makes it abundantly clear to the human reader
> > that a block follows.

>
> But it's too late by then. You have to mentally backtrack.
>
> blah blah blah blah <statement is complete>
> indented block <surprise the previous line wasn't complete>
>
> blah blah blah blah colon <statement is not complete>
> indented block


Source code is (unlike normal text) not read line by line. So you (at
least I) don't have to backtrack from line 2 to line 1 because you see
them both at the same time.

Thorsten
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      07-13-2011
On Wed, Jul 13, 2011 at 9:07 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> The colon indicates that the sentence has more to follow: I think of it as a
> pointer. It doesn't finish the thought, like a full stop, nor is it a mere
> pause, like a comma or semi-colon.
>
> * *An indented block on its own is surprising. It just hangs there,
> * *with no connection to what was going on before. Why is it indented?
> * *Is it connected to the previous sentence?


It's not necessarily surprising, depending on context; the brain
automatically assumes that the indented block was originally said by
someone else.

The <blockquote> tag defines a long quotation.

A browser inserts white space before and after a blockquote
element. It also insert margins for the blockquote element.

http://www.w3schools.com/tags/tag_blockquote.asp

(I didn't need to quote that, I just wanted to go meta and block quote
something about blockquote.)

Having the colon makes it clear that the content is part of the same
general thought (paragraph or sentence).

Chris Angelico
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      07-13-2011
On 07/13/2011 06:26 AM, Thorsten Kampe wrote:
> Source code is (unlike normal text) not read line by line. So
> you (at least I) don't have to backtrack from line 2 to line 1
> because you see them both at the same time.


$a
You mean there are people who don't use "ed" to write their code?

-tkc
..
w
q






 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      07-13-2011
On 2011-07-13, Thorsten Kampe <(E-Mail Removed)> wrote:

>> and that that block is to be considered in relation to what was just
>> said, before the colon.

>
> The indentation makes it abundantly clear to the human reader that
> that indented block is to be considered in relation to what was just
> said, before the indentation.


You would think so, but human readers like redundancy.

Most natural human languages have plenty of redundancy. For example
in English when speaking of multiple subjects one not only uses a
plural noun or pronoun (e.g. "they" rather than "him"), but one also
uses a plural verb ("run" rather than "runs") even though the plural
noun alone should make it abundantly clear to the human reader than
we're talking about more than one person. The same holds true for
objective and subjective case: the position of the noun in the
sentence makes it abundantly clear whether the noun is an object or a
subject, yet we still often have two cases (it's "I run" rather than
"Me run").

--
Grant Edwards grant.b.edwards Yow! I'm having an
at EMOTIONAL OUTBURST!! But,
gmail.com uh, WHY is there a WAFFLE
in my PAJAMA POCKET??
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      07-13-2011
On Wed, Jul 13, 2011 at 11:03 PM, Grant Edwards <(E-Mail Removed)> wrote:
> You would think so, but human readers like redundancy.
>


One of the benefits of redundancy is error-trapping. If you see a list
of numbers like this:

40
14
24
56
48
12
60
16
=====
269

then you know the result can't be right, because they're all even
numbers and the total isn't. The redundancy of having both the string
of numbers and the total adds only a small amount to the transmission
requirement, but it adds a lot to the reliability of transfer.

ChrisA
 
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
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 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
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Unable to open web project. A rogue colon ':' is appearing in the =?Utf-8?B?Z3JlZyBz?= ASP .Net 1 01-18-2005 07:58 PM
regex: colon screwing up my pattern? Darrel ASP .Net 0 12-28-2004 12:56 PM



Advertisments