Python Quiz

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Chermside

    Python Quiz

    richardc writes:[color=blue]
    > Im no expert but wouldnt you accept that Python has 'borrowed' FORTRAN's
    > fixed format syntax. I can only think of Python and FORTRAN off the top of
    > my head which use whitespace as part of the syntax.[/color]

    Definitely NOT. Without addressing the question of HOW Python came by the
    idea of using indentation to indicate block structure, it clearly couldn't
    have been from Fortran, because what Python does and what Fortran does
    are COMPLETELY different.

    In Fortran, starting lines in particular columns is meaningful (as are other
    particular characters in particular leading columns). In Python, particular
    columns have NO meaning. In fact, I would venture to say that it is
    NOT whitespace that is "significan t" in Python... it's INDENTATION. The
    distinction is significant, because humans are very poor at reading
    whitespace (the presense or absence of it yes, but the amount is something
    we're not good at reading), but we find indentation to be a *very* readable
    way to mark block structure (in practically every language which doesn't
    (like Fortran) prohibit indentation, it is a convention adhered to
    universally ).

    There's also the fact that "lines" (statements) are defined by line breaks
    in Python -- but I rarely hear complaints about this, since most programmers
    mentally chunk up code into "lines" anyway.

    Of course, I'm not suggesting that whitespace is *meaningless* in Python
    outside of indentation... Python is much like C (and many, many others) in
    that regard. After all, in C "y=x+ ++z" and "y=x++ +z" are quite
    different things. And in both Python and C "x=abc" is legal, but "x=a bc"
    is not.

    So I would say MOST languages make the presence or absence of whitespace
    significant... and that's a good idea, since it helps humans "tokenize"
    the code they read. Python uses newlines to indicate where new "lines"
    (statements) begin... not usually a controversial convention. Finally,
    most PROGRAMMERS use indentation to indicate block structure, but Python
    differs from most languages in that the Python parser uses the indentation
    level as syntax for blocks, while most languages don't. And Python
    doesn't[1] care about the AMOUNT of white space (independent of indentation
    level) which is a good thing, because humans find that difficult to
    read.

    -- Michael Chermside

    [1] Unless you mix tabs and spaces. Don't do that. Use tabnanny if you
    need to.


  • Mark Jackson

    #2
    Re: Python Quiz

    Michael Chermside <mcherm@mcherm. com> writes:[color=blue]
    > richardc writes:[color=green]
    > > Im no expert but wouldnt you accept that Python has 'borrowed' FORTRAN's
    > > fixed format syntax. I can only think of Python and FORTRAN off the top of
    > > my head which use whitespace as part of the syntax.[/color]
    >
    > Definitely NOT. Without addressing the question of HOW Python came by the
    > idea of using indentation to indicate block structure, it clearly couldn't
    > have been from Fortran, because what Python does and what Fortran does
    > are COMPLETELY different.
    >
    > In Fortran, starting lines in particular columns is meaningful (as are other
    > particular characters in particular leading columns). In Python, particular
    > columns have NO meaning.[/color]
    [color=blue]
    > Of course, I'm not suggesting that whitespace is *meaningless* in Python
    > outside of indentation... Python is much like C (and many, many others) in
    > that regard. After all, in C "y=x+ ++z" and "y=x++ +z" are quite
    > different things. And in both Python and C "x=abc" is legal, but "x=a bc"
    > is not.[/color]

    Note that Fortran (at least historic Fortran - not sure about those
    upstart 9x variants) is *not* among the "many, many others." One can
    write any of

    DO 10 I = something
    DO10I = something
    D O 1 0 I = something

    and leave it to the compiler to figure out whether you're starting a
    DO-loop or assigning a value to the variable DO10I.

    [Signature from February 1993.]

    --
    Mark Jackson - http://www.alumni.caltech.edu/~mjackson
    Consistently separating words by spaces became a general custom
    about the tenth century A.D., and lasted until about 1957, when
    FORTRAN abandoned the practice.
    - Sun FORTRAN Reference Manual



    Comment

    • richardc

      #3
      Re: Python Quiz

      Sorry, I didnt mean to suggest for a second that Python's use of indentation
      (ws), is a problem or in any way a labour of love like FORTRAN's.



      "Michael Chermside" <mcherm@mcherm. com> wrote in message
      news:mailman.10 58359706.27031. [email protected] ...
      SNIP[color=blue]
      > Definitely NOT. Without addressing the question of HOW Python came by the
      > idea of using indentation to indicate block structure, it clearly couldn't
      > have been from Fortran, because what Python does and what Fortran does
      > are COMPLETELY different.[/color]

      Very true, but to say that FORTRAN and Python have nothing in common would
      also be untrue. They both use ws to delimit syntatic elements, not the same
      elements but use it in a very different way to most other languages.

      I admit that Python probley gained no insiration from FORTRAN and any
      'similarities' are coincidental.
      [color=blue]
      > There's also the fact that "lines" (statements) are defined by line breaks
      > in Python -- but I rarely hear complaints about this, since most[/color]
      programmers[color=blue]
      > mentally chunk up code into "lines" anyway.[/color]

      No complaints there
      [color=blue]
      > Of course, I'm not suggesting that whitespace is *meaningless* in Python
      > outside of indentation...[/color]

      should hope not

      Please dont misinterprit my post as an attack on Python, whilst Im a old-hat
      C++ programmer and first time I looked at Pythno I thought... urggh, no
      brackets thats horrid. After a bit better look, I love it. Like everything
      it has its place.

      Sorry if I upset anyone... I know FORTRAN's bad, but its not that bad...
      erm, actually mabe it is ?

      As for OCCAM, I havent programmed in that for years, that was fun *lost in
      fond memories*

      Rich



      Comment

      • Peter Hansen

        #4
        Re: Python Quiz

        Mark Jackson wrote:[color=blue]
        >
        > Michael Chermside <mcherm@mcherm. com> writes:[color=green]
        > > Of course, I'm not suggesting that whitespace is *meaningless* in Python
        > > outside of indentation... Python is much like C (and many, many others) in
        > > that regard.[/color]
        >
        > Note that Fortran (at least historic Fortran - not sure about those
        > upstart 9x variants) is *not* among the "many, many others." One can
        > write any of
        >
        > DO 10 I = something
        > DO10I = something
        > D O 1 0 I = something
        >
        > and leave it to the compiler to figure out whether you're starting a
        > DO-loop or assigning a value to the variable DO10I.[/color]

        If you are saying that you leave it up to the compiler to decide how
        to interpret any of the three above statements, then clearly whitespace
        is *not* meaningless. It might be compiler-dependent or something,
        but no meaningless. Unless you are saying that on a given FORTRAN
        compiler, only one possible interpretation of all three statements
        above is possible. That would be surprising.

        -Peter

        Comment

        • Grant Edwards

          #5
          Re: Python Quiz

          In article <3F156952.AD24A [email protected] >, Peter Hansen wrote:
          [color=blue][color=green][color=darkred]
          >>> Of course, I'm not suggesting that whitespace is *meaningless*
          >>> in Python outside of indentation... Python is much like C (and
          >>> many, many others) in that regard.[/color]
          >>
          >> Note that Fortran (at least historic Fortran - not sure about
          >> those upstart 9x variants) is *not* among the "many, many
          >> others." One can write any of
          >>
          >> DO 10 I = something
          >> DO10I = something
          >> D O 1 0 I = something
          >>
          >> and leave it to the compiler to figure out whether you're
          >> starting a DO-loop or assigning a value to the variable DO10I.[/color]
          >
          > If you are saying that you leave it up to the compiler to
          > decide how to interpret any of the three above statements, then
          > clearly whitespace is *not* meaningless.[/color]

          IIRC (it's been a _long_ time) it's not up to the compiler. All
          three of the above are required to be treated the same by the
          compiler (a DO loop).
          [color=blue]
          > It might be compiler-dependent or something, but no
          > meaningless. Unless you are saying that on a given FORTRAN
          > compiler, only one possible interpretation of all three
          > statements above is possible.[/color]

          I don't think it's "on a given FORTRAN compiler", I think it's
          more like "according to the language definition for FORTRAN IV".
          [color=blue]
          > That would be surprising.[/color]

          Yup. I found a lot of things in FORTRAN surprising. Python is
          so much nicer in that respect. Though I was recently surprised
          that the remove() method for lists uses "==" and not "is" to
          determine what to remove. It's documented that it works that
          way. But, it wasn't what I excpected, and it took me a while to
          figure out that it was using my class's __cmp__ method rather
          than the object ID.

          --
          Grant Edwards grante Yow! Go on, EMOTE! I
          at was RAISED on thought
          visi.com balloons!!

          Comment

          • Peter Hansen

            #6
            Re: Python Quiz

            Grant Edwards wrote:[color=blue]
            >
            > Though I was recently surprised
            > that the remove() method for lists uses "==" and not "is" to
            > determine what to remove. It's documented that it works that
            > way. But, it wasn't what I excpected, and it took me a while to
            > figure out that it was using my class's __cmp__ method rather
            > than the object ID.[/color]

            Probably a very good thing, considering what would happen if
            you were trying to remove strings from the list, rather than
            simple things like integers...

            -Peter

            Comment

            • Mark Jackson

              #7
              Re: Python Quiz

              Peter Hansen <peter@engcorp. com> writes:[color=blue]
              > Mark Jackson wrote:[color=green]
              > >
              > > Michael Chermside <mcherm@mcherm. com> writes:[color=darkred]
              > > > Of course, I'm not suggesting that whitespace is *meaningless* in Python
              > > > outside of indentation... Python is much like C (and many, many others) in
              > > > that regard.[/color]
              > >
              > > Note that Fortran (at least historic Fortran - not sure about those
              > > upstart 9x variants) is *not* among the "many, many others." One can
              > > write any of
              > >
              > > DO 10 I = something
              > > DO10I = something
              > > D O 1 0 I = something
              > >
              > > and leave it to the compiler to figure out whether you're starting a
              > > DO-loop or assigning a value to the variable DO10I.[/color]
              >
              > If you are saying that you leave it up to the compiler to decide how
              > to interpret any of the three above statements, then clearly whitespace
              > is *not* meaningless. It might be compiler-dependent or something,
              > but no meaningless. Unless you are saying that on a given FORTRAN
              > compiler, only one possible interpretation of all three statements
              > above is possible. That would be surprising.[/color]

              You misunderstand, probably because I didn't express myself clearly.
              Again from the Sun Fortran Reference Manual, "Special characters used
              for punctuation," after the graphic used in the manual for the space
              character: "Ignored in statements, except as part of a character
              constant."

              Ignored means *ignored*, the three examples given will be parsed
              identically and the meaning will be determined using other
              information. I think in this case the nature of "something" should be
              determinative, as the syntax for the range and stride of a DO-loop

              start, stop [,stride]

              doesn't match any legal expression (which is what would be required if
              this were assignment to the variable DO10I.

              But it's perfectly legal, if insane, to declare (or, with implicit
              typing undefeated, just go ahead and use) a variable named DO10I and
              write, say

              DO 10 I = 1.10

              because in Fortran whitespace is *not* considered to delimit tokens.

              --
              Mark Jackson - http://www.alumni.caltech.edu/~mjackson
              It is necessary to be slightly underemployed if you want to
              do something significant. - James D. Watson


              Comment

              • Grant Edwards

                #8
                Re: Python Quiz

                In article <3F1573FB.8ADBE [email protected] >, Peter Hansen wrote:
                [color=blue][color=green]
                >> Though I was recently surprised that the remove() method for
                >> lists uses "==" and not "is" to determine what to remove. It's
                >> documented that it works that way. But, it wasn't what I
                >> excpected, and it took me a while to figure out that it was
                >> using my class's __cmp__ method rather than the object ID.[/color]
                >
                > Probably a very good thing, considering what would happen if
                > you were trying to remove strings from the list, rather than
                > simple things like integers...[/color]

                I didn't mean to imply that the way it's done isn't a good
                idea, or that what I expected wasn't a bad idea. It's just
                novel to be surprised by Python -- which wasn't the case with
                FORTRAN.

                --
                Grant Edwards grante Yow! My nose feels like a
                at bad Ronald Reagan movie...
                visi.com

                Comment

                • Raymond Hettinger

                  #9
                  Re: Python Quiz

                  > I admit that Python probley gained no insiration from FORTRAN and any[color=blue]
                  > 'similarities' are coincidental.[/color]

                  Didn't FORTRAN popularise the idea of having
                  the language separate from tons of special purpose
                  libraries?


                  Raymond Hettinger


                  Comment

                  • Terry Reedy

                    #10
                    Re: Python Quiz


                    "Grant Edwards" <[email protected] m> wrote in message
                    news:3f156c23$0 $166$a1866201@n ewsreader.visi. com...[color=blue]
                    > Yup. I found a lot of things in FORTRAN surprising. Python is
                    > so much nicer in that respect. Though I was recently surprised
                    > that the remove() method for lists uses "==" and not "is" to
                    > determine what to remove. It's documented that it works that
                    > way. But, it wasn't what I excpected, and it took me a while to
                    > figure out that it was using my class's __cmp__ method rather
                    > than the object ID.[/color]

                    Given ints = [9, 99, 999, 9999] would it not surprise you even more if
                    ints.remove(99) worked and ints.remove(999 ) did not, or even worse, if
                    the behavior of ints.remove(99) depended on the implementation?
                    (Similar examples apply to strings, where the implementation of
                    interning and hence behavior based on identity *has* changed!)

                    Terry


                    Comment

                    • Dennis Lee Bieber

                      #11
                      Re: Python Quiz

                      Michael Chermside fed this fish to the penguins on Wednesday 16 July
                      2003 05:47 am:
                      [color=blue]
                      > richardc writes:[color=green]
                      >> Im no expert but wouldnt you accept that Python has 'borrowed'
                      >> FORTRAN's
                      >> fixed format syntax. I can only think of Python and FORTRAN off the
                      >> top of my head which use whitespace as part of the syntax.[/color]
                      >
                      > Definitely NOT. Without addressing the question of HOW Python came by
                      > the idea of using indentation to indicate block structure, it clearly
                      > couldn't have been from Fortran, because what Python does and what
                      > Fortran does are COMPLETELY different.
                      >[/color]
                      When it comes to FORTRAN, other than the reserved columns for branch
                      label, continuation, and "sequence number", FORTRAN totally IGNORES
                      white-space...

                      D O 1 0I=1, 3 0, 2
                      and
                      DO 10 I = 1, 30, 2

                      are IDENTICAL to a FORTRAN compiler.

                      --[color=blue]
                      > =============== =============== =============== =============== == <
                      > [email protected] om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > [email protected] | Bestiaria Support Staff <
                      > =============== =============== =============== =============== == <
                      > Bestiaria Home Page: http://www.beastie.dm.net/ <
                      > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                      Comment

                      • Dennis Lee Bieber

                        #12
                        Re: Python Quiz

                        Grant Edwards fed this fish to the penguins on Wednesday 16 July 2003
                        08:15 am:

                        [color=blue]
                        >
                        > IIRC (it's been a _long_ time) it's not up to the compiler. All
                        > three of the above are required to be treated the same by the
                        > compiler (a DO loop).
                        >[/color]
                        It depends on the "something"

                        DO10I=1.3 is an assignment
                        DO10I=1,3 is a DO loop. And the compiler can't tell
                        the difference until it reaches the . or , (and if it guessed wrong, it
                        has to backtrack to the beginning of the statement and reparse).

                        Spacing, however, is not signicant.

                        D O10 I = 1 . 3 is valid for the first one.
                        D O10 I = 1 , 3 is identical to the second above.

                        --[color=blue]
                        > =============== =============== =============== =============== == <
                        > [email protected] om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                        > [email protected] | Bestiaria Support Staff <
                        > =============== =============== =============== =============== == <
                        > Bestiaria Home Page: http://www.beastie.dm.net/ <
                        > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                        Comment

                        Working...