Python Quiz

M

Michael Chermside

richardc said:
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.

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 "significant" 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.
 
M

Mark Jackson

Michael Chermside said:
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.
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.

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
 
R

richardc

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.



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

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

No complaints there
Of course, I'm not suggesting that whitespace is *meaningless* in Python
outside of indentation...

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
 
P

Peter Hansen

Mark said:
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.

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
 
G

Grant Edwards

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.

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

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".
That would be surprising.

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

Mark Jackson

Peter Hansen said:
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.

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

Peter Hansen

Grant said:
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.

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
 
G

Grant Edwards

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

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

Raymond Hettinger

I admit that Python probley gained no insiration from FORTRAN and any
'similarities' are coincidental.

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


Raymond Hettinger
 
T

Terry Reedy

Grant Edwards said:
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.

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
 
D

Dennis Lee Bieber

Michael Chermside fed this fish to the penguins on Wednesday 16 July
2003 05:47 am:
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.
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.

--
 
D

Dennis Lee Bieber

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

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

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top