Is it possible to escape a reserved word in Python?
Asked Answered
P

6

12

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 @

Paralogism answered 28/6, 2011 at 8:50 Comment(0)
M
20

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
Moore answered 28/6, 2011 at 9:15 Comment(4)
This says we can . para3 3rd last line developers.google.com/edu/python/stringsBunker
I can't find anything there on escaping reserved words, only a bit about how you shouldn't use len as a variable name (and len is not a keyword).Moore
python allowed my to create a list named "list" , it should not , no idea why python allowed me to user reserved words.Unbelt
@etl_devs list is not a reserved word, it is simply the name of a builtin. Shadowing those is generally a bad idea, but it's perfectly legal.Moore
P
7

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!

Paralogism answered 28/6, 2011 at 8:52 Comment(5)
although if you add any character to a keyword on either side as long as the result is a valid identifer, it's now distinct from the keyword. i.e. in is a keyword but in2 and xin are not.Karriekarry
This says we can but shouldn't. para3 3rd last line developers.google.com/edu/python/stringsBunker
How on earth do you add an XML attribute with the identified "class" then?Springtime
@aWebDeveloper: if you mean the reference to "len", that is not a keyword, but a pre-defined function, so it can be redefined at will.Readership
@Springtime For "class": #26765134 (posted by Manuel ALberto Snchez Hernndez)Rucker
L
2

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.

Lubricous answered 28/6, 2011 at 9:4 Comment(1)
You typically don't want to prefix, because a prefixed underscore typically refers to "non-public" attributes. Postfixing is probably better.Hyla
S
1

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.

Sydney answered 23/8, 2018 at 16:30 Comment(0)
P
1
db.call( ..., **{ 'from' : date_from }, **{ 'to' : date_to })

Python doesn't do those check when unpacking.

Punctilious answered 12/11, 2019 at 11:9 Comment(0)
J
0

Right now I was trying to understand what was wrong with my code, since I was not able to print something.

After seeking 'print-by-print', I found my error and I think it may be a Bug...

I'm using Jupyter notebook / Python=3.9. It was something like:

>>> print = ('Check exit for this loop.')

By mistake an "=" was kept by me and the command 'print' worked as a variable.

If I run isolately print, returns its content as a variable.

>>>print
'Check exit for this loop.'

I supposed that 'print' was a reserved word and due the error on my code I learned now that it is not the case.

Juliannajulianne answered 28/4 at 23:27 Comment(4)
I'm not following how this answers the question? Here in the documentation for Python under 'Keywords' lists the reserved words or keywords. print is not among them. Here tells you how to check them if you first run import keyword. Specifically, import keyword then on the next line keyword.iskeyword("print") or keyword.issoftkeyword("print") both give your False. By contrast keyword.iskeyword("if") gives you True. You are free to clobber Python's print ...Refugee
<continued> by assigning it to something else, as you found. (You can do that with a lot of items that are in the core namespace of Python, though it is bad idea and is something to be vigilant about avoiding.) However, your finding that such clobbering (or 'shadowing / shadow naming') is possible, as it is not blocked by Python, doesn't address the question in the original post. Along this line, something to look at for related to this is the hidden state you can make in a Jupyter notebook. Imagine you did this assignment of print that is in your example as a cell in a notebook, and then...Refugee
<continued> you caught yourself and decided that was a bad idea. And then you delete that cell. While you deleted the cell with the assignment, if you executed the cell before you deleted it, you'd have already made the assignment and upon deletion it would be only in the hidden state of the current active notebook. None of the code reflects that has been run. So the best thing to do to avoid this pitfall of the hidden state is to get in the habit of restarting the kernel regularly and running all the cells. This way you more quickly catch doing such a thing while you remember how you did it.Refugee
Dear @Wayne, Thank you very much for your gentle comment and explanation, I will take care and remember. Cheers...Juliannajulianne

© 2022 - 2024 — McMap. All rights reserved.