Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to escape a reserved word in Python?

Tags:

python

syntax

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 @

like image 884
Daniel Fortunov Avatar asked Jun 28 '11 08:06

Daniel Fortunov


2 Answers

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
like image 132
Jasmijn Avatar answered Nov 15 '22 21:11

Jasmijn


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

Python doesn't do those check when unpacking.

like image 27
UltimateDRT Avatar answered Nov 15 '22 20:11

UltimateDRT