Is it possible to escape a reserved word in Python?

Viewed 15112

It may not be a good idea to name a variable after a reserved word, but I am curious:

Is there any escape syntax in Python to allow you to use a reserved word as the name of a variable?

For example, in C# this can be done by prefixing the reserved word with @

5 Answers

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.

db.call( ..., **{ 'from' : date_from }, **{ 'to' : date_to })

Python doesn't do those check when unpacking.

Related