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

Discussion in 'Python' started by Thomas Jollans, Jul 11, 2011.

  1. 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/2011/07/karin-dewar-indentation-and-colon.html
     
    Thomas Jollans, Jul 11, 2011
    #1
    1. Advertising

  2. On 2011-07-11, Thomas Jollans <> 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
     
    Grant Edwards, Jul 11, 2011
    #2
    1. Advertising

  3. Thomas Jollans

    alex23 Guest

    alex23, Jul 13, 2011
    #3
  4. Re: An interesting beginner question: why we need colon at all in the python language?

    * 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
     
    Thorsten Kampe, Jul 13, 2011
    #4
  5. Re: An interesting beginner question: why we need colon at all in the python language?

    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
     
    Steven D'Aprano, Jul 13, 2011
    #5
  6. Re: An interesting beginner question: why we need colon at all in the python language?

    * 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
     
    Thorsten Kampe, Jul 13, 2011
    #6
  7. Re: An interesting beginner question: why we need colon at all in thepython language?

    On Wed, Jul 13, 2011 at 9:07 PM, Steven D'Aprano
    <> 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
     
    Chris Angelico, Jul 13, 2011
    #7
  8. Thomas Jollans

    Tim Chase Guest

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

    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
     
    Tim Chase, Jul 13, 2011
    #8
  9. Re: An interesting beginner question: why we need colon at all inthe python language?

    On 2011-07-13, Thorsten Kampe <> 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??
     
    Grant Edwards, Jul 13, 2011
    #9
  10. Re: An interesting beginner question: why we need colon at all in thepython language?

    On Wed, Jul 13, 2011 at 11:03 PM, Grant Edwards <> 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
     
    Chris Angelico, Jul 13, 2011
    #10
  11. Re: An interesting beginner question: why we need colon at all inthe python language?

    On 2011-07-13, Steven D'Aprano <> wrote:
    > 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!


    Except that's wrong English[1]. At least that's not correct usage
    according to what I learned in school. A colon follows an independent
    clause: something that could in essence be a complete sentence and
    expresses a complete thought. The colon separates that independent
    clause from examples or an explanation or clarification of that
    independent clause. The phrase "Our three weapons are" isn't an
    independent clause. It has the transitive verb "are" but no predicate
    nominative. You've placed the colon in the middle of an independent
    clause between the verb "are" and the predicate nominative phrase
    "fear, surprise, and ruthless efficiency, and an almost fanatical
    devotion to the Pope".

    Your example should be something like this [accurace of the quotation
    aside]:

    The Spanish Inquisition has three main weapons: fear, surprise,
    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,


    Something is missing. It's not a complete independent clause.

    > as if I had started to write something and forgotten to finish.


    Except a colon doesn't "complete" an independent clause that's
    otherwise incomplete.

    > It needs a colon to be complete:
    >
    > Nobody expects the Spanish Inquisition! <JARRING CHORDS>


    Now, that usage is correct.

    > The colon indicates that the sentence has more to follow: I think of
    > it as a pointer.


    That is correct also.

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


    Yup.

    [1] Since all posts criticising grammar or spelling will have an above
    average number of grammar and spelling errors, I thought I'd get
    a head start on it.

    --
    Grant Edwards grant.b.edwards Yow! I'm ZIPPY the PINHEAD
    at and I'm totally committed
    gmail.com to the festive mode.
     
    Grant Edwards, Jul 13, 2011
    #11
  12. Thomas Jollans

    Terry Reedy Guest

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

    On 7/13/2011 2:26 AM, alex23 wrote:
    > Thomas Jollans<> 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/2011/07/karin-dewar-indentation-an...

    >
    > It's also part of the Python FAQ:
    >
    > http://docs.python.org/faq/design.html#why-are-colons-required-for-the-if-while-def-class-statements


    An added note: the header lines of compound statements do not
    necessarily occupy just one physical line. The : signals the end of the
    logical line. Editors can use to to indent intelligently. Consider

    def myfunc(a,
    b,
    c):
    return a+b+c

    All indentation was done automatically by IDLE's editor.

    --
    Terry Jan Reedy
     
    Terry Reedy, Jul 13, 2011
    #12
  13. Re: An interesting beginner question: why we need colon at all in the python language?

    * Grant Edwards (Wed, 13 Jul 2011 13:03:22 +0000 (UTC))
    > On 2011-07-13, Thorsten Kampe <> 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.


    I also like redundancy (and consistency). That's why I'd much more
    prefer a "then" than a colon which is easily overlooked while reading
    /and/ while writing.

    Thorsten
     
    Thorsten Kampe, Jul 13, 2011
    #13
  14. Re: An interesting beginner question: why we need colon at all inthe python language?

    On 2011-07-13, Thorsten Kampe <> wrote:
    > * Grant Edwards (Wed, 13 Jul 2011 13:03:22 +0000 (UTC))
    >> On 2011-07-13, Thorsten Kampe <> 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.

    >
    > I also like redundancy (and consistency). That's why I'd much more
    > prefer a "then" than a colon which is easily overlooked while reading
    > /and/ while writing.


    How is the "then" going to be consistent with other things that also
    introduce blocks (def, try, with, etc.).

    --
    Grant Edwards grant.b.edwards Yow! ! I'm in a very
    at clever and adorable INSANE
    gmail.com ASYLUM!!
     
    Grant Edwards, Jul 14, 2011
    #14
  15. Thomas Jollans

    Wanderer Guest

    Re: An interesting beginner question: why we need colon at all in thepython language?

    On Jul 14, 10:34 am, Grant Edwards <> wrote:
    > On 2011-07-13, Thorsten Kampe <> wrote:
    >
    > > * Grant Edwards (Wed, 13 Jul 2011 13:03:22 +0000 (UTC))
    > >> On 2011-07-13, Thorsten Kampe <> 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.

    >
    > > I also like redundancy (and consistency). That's why I'd much more
    > > prefer a "then" than a colon which is easily overlooked while reading
    > > /and/ while writing.

    >
    > How is the "then" going to be consistent with other things that also
    > introduce blocks (def, try, with, etc.).
    >
    > --
    > Grant Edwards               grant.b.edwards        Yow! !  I'm in a very
    >                                   at               clever and adorable INSANE
    >                               gmail.com           ASYLUM!!


    But if you have the colon, why do you need the brackets or backslashes
    in an if statement.

    Why not

    if condition1 or
    condition2 or
    condition3:
    do_something()

    The statement ain't over til there's a colon.
     
    Wanderer, Jul 14, 2011
    #15
  16. Re: An interesting beginner question: why we need colon at all in the python language?

    Wanderer wrote:

    > But if you have the colon, why do you need the brackets or backslashes
    > in an if statement.
    >
    > Why not
    >
    > if condition1 or
    > condition2 or
    > condition3:
    > do_something()
    >
    > The statement ain't over til there's a colon.



    Because there are virtues in having the parser be nice and simple. Syntax
    constraints help identify errors:

    mystr = "this is a string

    Should we say that no closing quote is needed, because the newline
    unambiguously ends the string? Well, perhaps... but allowing such a rule
    would mask errors:

    mystr = "this is a %s % type(something)

    Good language design requires constraints on what is allowed as well as
    freedom from unnecessary syntax.

    The appropriate lines from the Zen are

    Errors should never pass silently.
    Unless explicitly silenced.

    Newlines end parsing of the current token or expression. Including a newline
    inside an expression is an error, unless you explicitly silence it by using
    a backslash or using brackets. It's a bit too far to say that "any if
    statement is an explicit way to silent newline errors".



    --
    Steven
     
    Steven D'Aprano, Jul 16, 2011
    #16
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    591
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,966
    Smokey Grindel
    Dec 2, 2006
  3. Sébastien de Mapias
    Replies:
    16
    Views:
    491
    Army1987
    Feb 14, 2008
  4. Zachary Ware
    Replies:
    0
    Views:
    113
    Zachary Ware
    Sep 18, 2013
  5. dieter
    Replies:
    0
    Views:
    106
    dieter
    Sep 19, 2013
Loading...

Share This Page