Almost keywords

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Jul 21 10:01:51 EDT 2008


Once in a while I feel free to write about less defined things, saying
mostly wrong things. This post is mostly chat, if you aren't
interested please ignore it.

Python is fit enough for newbie programmers, but some of its
characteristics can confuse them still, like the variables referenced
by name (that can be solved with a language that gives the illusion of
always manging values), the finite nature of floating point numbers
(that may be partially solved with interval arithmetic used in a smart
way, even if it may end being quite slower than Python floats), the
case sensitivity of all the language (in natural languages the case is
often irrelevant), the absence of rational numbers from integer
divisions, the code formatting precision required by significant
indentations, the "artificial" difference between lazy and eager
things, the silly syntax of tuples, some problems with closures that
partially come from automatic creation of variable names, many
inconsistencies present in the standard library, etc. Python 3.0
solved some of those problems, especially the smaller ones (for
example the "nonlocal" keyword workaround, etc).

If you take a look at the Python newsgroup, you can see that newbies
there do many similar mistakes all the time. A language fit for
newbies can adapt itself to avoiding those most common mistakes (but
today Python is becoming larger and more complex, more fit for real
programmers and less for programming newbies. This may be inevitable
in the evolution of a a language with commercial success). One of the
mistakes you see often is the rebinding of built-in things, like map,
list, etc. (Python 3.0 again turns some of those names into keywords,
avoiding part of those problems).

So you can see code like this that may lead to bugs in programs
written by newbies:
list = [x*x for x in range(100)

A possible way to avoid such bugs is to turn all those names like
"list", "map", "filter", "self", etc into keywords. But this may have
some disadvantages (well, I think I'd like to have "self" as keyword,
seen how all Python books strong suggest to not use a name different
from "self"). An intermediate solution is to change Python so that it
allows you to rebind those half-keywords only of you want it and you
know what you are doing, with a kind of comment at the top of the
module:

# rebinding = True
This allows you to do:
list = [x*x for x in range(100)]

By default rebinding is False, so this produces a SyntaxError:
list = [x*x for x in range(100)]

This may be a silly idea. Designing a good syntax for a language isn't
easy at all.

Note that a widely used Scheme implementation, DrScheme, implements
various sub-languages fit for newbies or almost-newbies or low-
experienced programmers. What is possible for experts can be a syntax
error for newbies. I don't like this feature much, I like more
freedom, but for purely learning purposes it may have its place. So
what I have said regarding the "almost keywords" isn't a totally new
idea :-)

Bye,
bearophile



More information about the Python-list mailing list