Is it possible to escape a reserved word in Python?

It is not possible, however it is some kind of a tradition in Python to append a _ to get a new identifier:

def drive(from_, to):
    pass

I received an error when using the following:

for r in db.call ('ReadCashflows',
                  number = number,
                  from = date_from,
                  to = date_to):

I tried using capitals instead and now it works:

for r in db.call ('ReadCashflows',
                  number = number,
                  FROM = date_from,
                  TO = date_to):

This was possible for me because my database is ORACLE (which is case-insensitive). Remark on the code above: in my software application the actual parameters in the database are pFROM and pTO; the "p" gets added in the post-processing before the call to the database is made.


No, this is not possible.

Section 2.3.1 of The Python Language Reference says that keywords 'cannot be used as ordinary identifiers' and does not specify an escape syntax.

This is probably a Good Thing, for the sake of code readability!


If you don't mind prefixing, you can "prefix" with an underscore. Sure it'll actually be part of the variable's name, but it'll look like a prefixed reserved word. Other than that, it is not possible.

Tags:

Python

Syntax