[PATCH] Identifiers with '?'

Neel Krishnaswami neelk at brick.cswv.com
Sun May 21 21:14:21 CEST 2000


At the risk of treading on the raging identifier-flamewar, I'd like to
present a one-line patch that brings one of Lisp's little syntactic
conveniences to Python -- the ability to make identifiers with a
question mark in the name. This is very useful for naming boolean
flags and functions.

An example will probably illustrate most easily.

Python 1.6a2 (#1, May 21 2000, 14:59:38)  [GCC 2.7.2.1] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
>>> import types
>>> types.IntType
<type 'int'>
>>> instance? = isinstance  # A var with '?' in the name
>>>
>>> instance?(3, types.IntType) # It can be used like any other name
1

I've restricted the use of '?' to the same status as numbers; you
can't start a variable name with '?', but it can be part of the follow
on. So 'foo?' is legal, but '?foo' isn't. (This is easy to change,
though.) 

Patch follows, against the 1.6 alpha in CVS. The change to 1.5.2
should be likewise obvious. Bon appetit!

Index: tokenizer.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Parser/tokenizer.c,v
retrieving revision 2.41
diff --context -r2.41 tokenizer.c
*** tokenizer.c 2000/05/03 23:44:37     2.41
--- tokenizer.c 2000/05/21 19:05:09
***************
*** 625,631 ****
                                goto letter_quote;
                        break;
                }
!               while (isalnum(c) || c == '_') {
                        c = tok_nextc(tok);
                }
                tok_backup(tok, c);
--- 625,631 ----
                                goto letter_quote;
                        break;
                }
!               while (isalnum(c) || c == '_' || c == '?') {
                        c = tok_nextc(tok);
                }
                tok_backup(tok, c);


Neel



More information about the Python-list mailing list