Python rocks

Alex Martelli aleax at mac.com
Sun Jun 3 11:56:08 EDT 2007


Mark Carter <me at privacy.net> wrote:

> Alex Martelli wrote:
> > Josiah Carlson <josiah.carlson at sbcglobal.net> wrote:
> > 
> >>> pitfall of Python is knowing whether an operation is destructive or not.
> >> If it returns None, it probably changes the content of an object.
> > 
> > A reasonable heuristic, but with lots of exceptions, alas:
> >     somedict.get(somekey)
> > will often return None without performing any change, while
> >     somelist.pop()
> > does modify somelist but typically returns non-None.
> > 
> > The use of trailing-exclamation-point (by convention) to indicate
> > "mutating methods" is a nice plus in languages that allow it.
> 
> Actually, that'd be nice to have in Python. And whilst we're about it,
> might as well go the whole hog and allow hyphens in names, too.

Hyphens look the same as minus signs, so that allowing them in names in
a language with infix operator syntax is far from a clear win.  Right
now, in Python and all cognate languages, a-b means a minus b; the
tectonic shift to having it be a 3-character identifier instead, in
addition to breaking millions of lines of existing code, would rightly
produce extreme resistance in anybody whose main programming experience
comes from Basic, C, Java, Fortran, C++, PL/I, Perl, C#, and Python
itself (and dozens of other languages, too, of course).

I noticed this effect when I was playing with Dylan (never did anything
"serious" with it, but I did like many aspects of the language): I kept
writing a-b (and for that matter a+b, etc) and getting weird syntax
errors because I had not interposed all needed spaces.  (If a-b is not
allowed as an expression, it makes sense that a+b isn't either).  

Despite substantial previous experience with Lisp, Scheme, and Forth, in
all of which a-b would "of course" be an identifier, that experience
just didn't guide my fingers -- in those languages operator are prefix
(Lisp, Scheme) or suffix (Forth), so it's "natural" that sticking an
operator sign "between" sequences of letters means nothing special...
but moving to an infix language just shifts my brain in a different
gear.  Cobol has preferably-prefix notation too, i.e., the natural way
to express the difference would be "SUBTRACT C FROM B", and the
alternative "algebraic" (infix) notation is and feels very "bolted-on",
so it's kind of an intermediate case, where hyphens are still OK.

Not only is the cost of making a-b an identifier extremely high, but the
gains are tiny: what conventional difference is there supposed to be
between a-b and a_b?  Looks like nothing but one more category of easily
confusable identifiers.  Big pain, little gain == not so great an idea.

Allowing a trailing ! in method names has no such cost, because in no
language I know is ! used as a "postfix unary operator"; the gain in the
convention "mutators end with !" is not huge, but substantial.  So, the
tradeoffs are different: small pain, substantial gain == not a bad idea.

However, this is all quite theoretical, because no more PEPs will be
accepted for Python 3000, so any language change like this would have to
wait for Python 4000, which is no doubt quite a distant prospect:-).


Alex



More information about the Python-list mailing list