Python Forum
does try really need to be a keyword?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does try really need to be a keyword?
#1
does try really need to be a keyword? perhaps not. maybe all that was needed was for try: to be a keyword. are there any uses of try without : following it? i can't think of any. but except might not be in the same situation since reference to exceptions can follow it.

this post is in the bar because it is the result of drinking too much ch3ch2oh. iow, it's not serious.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is a reason why 'try:' shouldn't be a keyword
>>> try        :
...     print('Hello')
... finally         :
...     print('world!')
... 
Hello
world!
>>> 
Reply
#3
that's still try followed by :.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
In Go, there was a proposal to implement try() for error handling. The community, self included, hated it, but we also don't care for more traditional error handling.

So, you could try to implement try as a try() function. Ironically, you'd likely end up using the try keyword in that function for the sake of simplicity.
Reply
#5
i'm just trying to understand why Python source code needs to use : and make the word used with it be a reserved word. it seems like syntactic overkill to me. for example, with try as a reserved word, it doesn't need the : to follow it. does anyone know of a case where try by itself would be ambiguous?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
skaperen Wrote:i'm just trying to understand why Python source code needs to use : and make the word used with it be a reserved word.
Try introduces an indented block of code as class, def, while, for do. Indented blocks of code have always been opened by a colon in python. This uniformity of the language explains the try:. You could as well argue that class and def don't need a colon.
Reply
#7
Quick reminder: https://python-forum.io/misc.php?action=help&hid=48

If you want a serious answer, you should probably try the mailing list.
Reply
#8
The colon before the indented blocks goes back to the ABC language where van Rossum found inspiration to write python. I don't think there is really a better reason.
Reply
#9
Colon is used to increase readability,also help editors/IDE's to automatically indent the next line if the previous line ended with a colon.
Python FAQ

The History of Python
Python’s use of indentation comes directly from ABC,
but this idea didn’t originate with ABC--it had already been promoted by Donald Knuth and was a well-known concept of programming style.
(The occam programming language also used it.)
However, ABC’s authors did invent the use of the colon that separates the lead-in clause from the indented block.
After early user testing without the colon,
it was discovered that the meaning of the indentation was unclear to beginners being taught the first steps of programming.
The addition of the colon clarified it significantly: the colon somehow draws attention to what follows and ties the phrases before and after it together in just the right way.

GVR Wrote:Now the Dewar story, which is how I got the idea of the colon,
as I wrote it down in a memoir of the ABC design rationale:
In 1978, in a design session in a mansion in Jabłonna (Poland),
Robert Dewar, Peter King, Jack Schwartz and Lambert were comparing various alternative proposed syntaxes for B,
by comparing (buggy) bubble sort implementations written down in each alternative.
Since they couldn't agree, Robert Dewar's wife was called from her room and asked for her opinion,
like a modern-day Paris asked to compare the beauty of Hera, Athena, and Aphrodite.
But after the first version was explained to her, she remarked: "You mean, in the line where it says: 'FOR i ... ',
that it has to be done for the lines that follow; not just for that line?!"
And here the scientists realized that the misunderstanding would have been avoided if there had been a colon at the end of that line.
Reply
#10
i'm not referring to how Python is today or how Python has always been. i'm referring to whether the language syntax needed to be this way. and this is not a proposal to change Python in any way. i am trying to understand the thinking behind the origin on the language. that might be the ABC language.

"The colon is required primarily to enhance readability"

if this is so then the lack of colon would detract from readability. that would not mean it has to be a part of the syntax. consider what message you get without it. does it say your code is poorly readable (by humans)?

"However, ABC’s authors did invent the use of the colon that separates the lead-in clause from the indented block"

with the colon we can form the structure. whatever we include with the colon can form the semantics. we can use "try" to mean a block of code that is under an exception catching umbrella. in order to do this, "try" and ":" must be together. "try" anywhere else would have no meaning. so why does is need to be a reserved word everywhere? why not just have it be a reserved word only when ":" is next (even when separated with spacing)?

(Jan-28-2020, 12:03 AM)Gribouillis Wrote: You could as well argue that class and def don't need a colon.
OK, i will, later. and "for" and "while", too. or i could, instead, argue, that ":" isn't needed, in general (and can be optional for enhancing readability for humans).

then, there's meaning in the indentation. but this is additional meaning if we give specific meaning to a word ("try" or "for") or to punctuation (":"). now we are getting even more redundancy in semantics.

if "try:" clearly expects a block to follow, ended by a statement that begins with "except" and ends with ":", then we already know where it begins and where it ends making the indentation redundant.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020