[Python-ideas] Making colons optional?
Riobard Zhan
yaogzhan at gmail.com
Thu Feb 5 04:05:30 CET 2009
Hi everybody,
I'm proposing to make colons optional. Let me call this feature
Colonless Python. To show what it is like without colons, here are
some examples adapted from the official tutorial.
---------- sample code -----------
if statement
============
if x < 0
x = 0
print 'Negative changed to zero'
elif x == 0
print 'Zero'
elif x == 1
print 'Single'
else
print 'More'
for statement
=============
for x in a
print x, len(x)
while statement
===============
# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1
while b < 10
print b
a, b = b, a+b
function definition
==================
def fib(n) # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n
print b,
a, b = b, a+b
class definition
================
class MyClass
"""A simple example class"""
i = 12345
def f(self)
return 'hello world'
------------- end --------------
Please note that like semicolons, one can still write one-liners with
colons like
if x > 0: y = z; x = y
or
for each in range(10): x += each; x -= each**2
I'm proposing this due to several reasons, namely
- I noticed a strong tendency to forget colons by new users of Python
in a second-year computer science undergraduate course. The students
seemed not getting used to colons even near the end of the course. I
guess it is probably because they learn Java and C first, both of
which do not have colons. What other languages do you know that
require colons? Colons seem to be a trap for new Python users from
other languages.
- We already have indentation to visually separate different levels of
code. Why bother with those extra colons at all? They should be
optional just like semicolons are optional (and discouraged) for line
breaks. I doubt we will ever lose much, if any, by omitting colons.
- I find colons pretty annoying. They interrupt mental flows of
thinking. I have to do a mental break at the end of conditionals,
loops, function and class definitions, and then a physical break to
type "SHIFT+;" to get the colons before I continue on the subsequent
lines. It's not really a big issue, but it is annoying.
What do you think? Specifically, may I ask your feedback on the
following issues?
- Do you find yourself sometimes forget to type them and the
interpreter complains?
- Are the above pieces of code less readable due to lack of colons?
- What problems do you think will occur if colons are made optional?
PS. I had some preliminary discussion in the comments on one of
Guido's blog posts about Python's history. If you are interested, you
can check out this link
http://python-history.blogspot.com/2009/01/pythons-design-philosophy.html#comments
Best,
Rio
More information about the Python-ideas
mailing list