Hello, This is not a proposal for change, rather a few reflexions I guess were worth being shared here, because this is a list where design questions about Python are debated. If you don't care, just press 'del'. I have always been puzzled by Python's usage of colons terminating block headlines. First, this does not match the overall noise-less design of the syntax; second, it compares weirdly with the absence of (required) semi-colons terminating instructions [1]. I stepped several times, in online articles, on vague evocations of "studies" supposed to "show" that this format is clearer to beginners, albeit each time without any reference to said studies [2]. Finally, I recently found what seems to be the actual story of ':' [3], telling that, according to _one_ non-programmer, colons should be added there to play the role of _introducing_ the following block, indented. This notion of _introduction_ endly clicked for me, making some sense. From a distinct perspective: another weird point is using '=' for assignment and '==' for equality [4]. In my view, the right assignment sign (rather: the sign to define a symbol, in general) is ':' precisely [5]. While thinking at syntax for a low-level language, I stepped on the special case of symbols which are labelled blocks (all targets of jumps/goto's, including procedures). In assembly and some other langs, they are usually written 'label:' or ':label:'. And indeed it is clear here that the label introduces the following block, with ':' obviously playing this introductory role. Why is it instead not obvious in Python [6]? Maybe the reason lies in the actual usage of colons, in fact in the actual headlines in python code. In assembly, one could have code looking like: jnz case-true ... (case false) ... case-true: ld rax result ... Here the label is a kind of title line for the block. There is a similar usage in python: def average (numbers): sum = numbers.sum() return sum / len(numbers) Again, here the headline is a kind of title. However, when the block is a control statement if/else, for, while, then the headline is rather an _articulation_ in the discourse: for n in numbers: print(n) while more: go_on() if cond: this() else that() From yet a third perspective: for a high-level language this time, which otherwise uses indented blocks like Python, I was searching for a nice syntax for single-line control blocks [7]. As in python's: for n in numbers: print(n) while cond: go_on() if cond: this() else: that() I first thought at using 'do': for n in numbers do print(n) while cond do go_on() if cond do this() else do that() if cond do this() else do that() which is very nice, I guess. [8] [9] Then, for whatever reason, came to my mind the following: for n in numbers, print(n) while cond, go_on() if cond, this() else, that() if cond, this() else, that() Just using ',', a bare comma. [10] It seems to me that this usage of the comma matches its meaning in ordinary (written) discourse. And there is indeed a question of discourse articulation. Here, of logical articulation, precisely. Maybe this is why, finally, Python's usage of the colon does not meaningfully click as it should: because most of the time it's not a question of a title introducing a block, but of discourse articulation; where ',' would do a better job, maybe. Note: I don't mean this post to introduce (sic!) endless violent discussions (as often, on such topics); and I won't reply if this happens. I wished to share a different view on the topic, for people who care about meaningful syntax. d [1] I think the parser should not an accept optional ';' at end of line. This alternative is never used, and, if it were, would only trouble novice programmers (What the heck does ';' mean? nothing...) [2] Which always made me laugh, knowing how supposed studies in the fields of psychology or sociology usually are highly contestable; so what about programmers or CS professionals driving such studies? [3] Someone will surely have a pointer ;-) [4] Forgetting ':' and using '=' for equality are my favorite syntax errors in Python. I seem to still love them, still after long years of frequentation. [5] Python uses it in dict notation, which is nice. But dict key->value links could use '->', while for symbol defs ':' or '↔' or '⟷' would be perfect. (Actually, the double head arrow is ideal, but unfortunately there is no such key on common keyboards.) [6] Not obvious at all, for me, instead I had to be told about it, I mean to read about it. Maybe it is non-obvious for most people, since this argument does not seem to come up in discussions about ':'. [7] The actual keywords are different, also the syntactic schema for procedure call, but I will use Python's syntax here in examples for readability. [8] However, syntax highlighting for keywords would help, here. [9] Not invent here: Lua uses 'do'; however also for multiline blocks, and only for loops, not for if/else. [10] This is even better in the case of this lang project, where like in Lisp collection items are just separated by spaces: no commas. Thus, the comma has a clear and single role in syntax.
On 13 February 2014 20:22, spir <denis.spir@gmail.com> wrote:
Hello,
This is not a proposal for change, rather a few reflexions I guess were worth being shared here, because this is a list where design questions about Python are debated. If you don't care, just press 'del'.
I have always been puzzled by Python's usage of colons terminating block headlines. First, this does not match the overall noise-less design of the syntax; second, it compares weirdly with the absence of (required) semi-colons terminating instructions [1]. I stepped several times, in online articles, on vague evocations of "studies" supposed to "show" that this format is clearer to beginners, albeit each time without any reference to said studies [2]. Finally, I recently found what seems to be the actual story of ':' [3], telling that, according to _one_ non-programmer, colons should be added there to play the role of _introducing_ the following block, indented. This notion of _introduction_ endly clicked for me, making some sense.
For your footnote 3: http://python-history.blogspot.com.au/2011/07/karin-dewar-indentation-and-co... :) There may also be more in the papers about ABC: http://homepages.cwi.nl/~steven/abc/publications.html (I skimmed the "Issues in the Design of a Beginners' Programming Language" one, which has some interesting parts about the indentation based syntax, but doesn't really discuss the value of the colons that introduce a new suite - I do personally like them though, as they provide a clear distinction between statement headers and multi-line expressions, especially when the header *includes* a multi-line expression)
From a distinct perspective: another weird point is using '=' for assignment and '==' for equality [4]. In my view, the right assignment sign (rather: the sign to define a symbol, in general) is ':' precisely [5]. While thinking at syntax for a low-level language, I stepped on the special case of symbols which are labelled blocks (all targets of jumps/goto's, including procedures). In assembly and some other langs, they are usually written 'label:' or ':label:'. And indeed it is clear here that the label introduces the following block, with ':' obviously playing this introductory role.
As far as I am aware, the =/== annotation is just an artefact of Python's C heritage, rather than being based on anything more profound.
Why is it instead not obvious in Python [6]?
I don't know if Guido actually designed it this way, but if you evaluate Python's original syntax as "simple statements are based on C, compound statements are based on ABC", a lot of things make more sense (and then the rest of the language evolved from there). So if someone comes to Python having learned a language like C, C++ or Java first, then it is only the ABC inspired bits (especially the indentation based suites) that will puzzle them. If a new user learns Python as their first programming language, then it all seems arbitrary anyway, so they have no reason to expect anything different. It requires expectations calibrated in a different environment (perhaps including formal mathematics?) to make the C-style =/== model seem particularly strange. Mathematicians may also be puzzled by the use of "j" for complex numbers (although electrical engineers will find that notation entirely familiar). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On 02/13/2014 12:31 PM, Nick Coghlan wrote:
For your footnote 3: http://python-history.blogspot.com.au/2011/07/karin-dewar-indentation-and-co... :)
Thank you!
There may also be more in the papers about ABC: http://homepages.cwi.nl/~steven/abc/publications.html (I skimmed the "Issues in the Design of a Beginners' Programming Language"
Read it as well.
[...]
Why is it instead not obvious in Python [6]?
I don't know if Guido actually designed it this way, but if you evaluate Python's original syntax as "simple statements are based on C, compound statements are based on ABC", a lot of things make more sense (and then the rest of the language evolved from there). So if someone comes to Python having learned a language like C, C++ or Java first, then it is only the ABC inspired bits (especially the indentation based suites) that will puzzle them. If a new user learns Python as their first programming language, then it all seems arbitrary anyway, so they have no reason to expect anything different.
This perspective makes sense. Although for beginners, precisely, I think using ',' is meaningful.
It requires expectations calibrated in a different environment (perhaps including formal mathematics?) to make the C-style =/== model seem particularly strange. Mathematicians may also be puzzled by the use of "j" for complex numbers (although electrical engineers will find that notation entirely familiar).
Yes, 'j' is not weird at all for me ;-) (background in automation, which also includes much of electrotechnics) d
Terminating a header says "I believe I have entered a syntactically correct header" The receiving system can use this to immediately do a header syntax check that would be invalid if performed too soon. The console and Idle Shell interactive interpreters console do this. Idle version: (note the English use of ':' to end a header ;-)
a = 3 if a == ( 3): pass
if a == (:
SyntaxError: invalid syntax Deleting or making the : optional is not compatibly with implicit line continuation. Explicit \ would have to be the only way to continue a logical header line on another physical line. ----- = versus ==: Info theory and common sense say that the more frequent indication should generally be given the shorter indication. Assignment is much more common that equality comparison. Math gets away with overloading = partly because 'binding' is a declaration of equality, partly because logic and arithmetic are usually kept separate, and partly because logical equivalence is indicated with a 3-line character when needed. Since that is not an ascii character, we use a 2 character digraph as replacement. -- Terry Jan Reedy
participants (4)
-
Greg Ewing
-
Nick Coghlan
-
spir
-
Terry Reedy