
Appendix C
Reserved Words and Identifiers in Python
Reserved Words
Python won't allow you to use any of its reserved words as variable or
function names. Here's the full list, with a very brief explanation.
-
and
-
Logical and
-
assert
-
Assert that some condition exists or is true
-
break
-
Break out of a while or for loop
-
class
-
Begin a class definition
-
continue
-
Immediately go to top of while or for loop
-
def
-
Begin a function definition
-
del
-
Delete (destroy) following object
-
elif
-
If clause
-
else
-
If clause
-
except
-
Execute following statements if try failed
-
exec
-
Run Python code
-
finally
-
Execute following statements if try succeeded
-
for
-
Begin a for loop
-
from
-
Begin a qualified import statement
-
global
-
Look in global namespace for following variable names
-
if
-
If clause
-
import
-
Find, read and execute a module
-
in
-
Membership in sequence
-
is
-
Identity query
-
lambda
-
Begin unnamed function definition
-
not
-
Logical not
-
or
-
Logical or
-
pass
-
Do nothing
-
print
-
Output
-
raise
-
Notify of exceptional condition
-
return
-
Exit function
-
try
-
Try the following statements and see if they work
-
while
-
Begin while loop
Reserved Identifiers
Certain classes of identifiers (variable or function names) have special
meanings. These are:
-
_
-
This variable is used in the interactive interpreter mode only.
When it exists, it stores the result of the last evaluation; the variable
lives in the __builtin__ module. If it does not exist, it has no
special meaning and is not defined. Leave this one to Python.
-
_*
-
Any variable beginning with an underscore is not imported by from module
import *. It's OK to create your own variables that begin with
one underscore, but you can only use it in the module where you created
it.
-
__*__
-
Any name beginning and ending with two underscores is defined by Python;
includes __main__, __import__, __add__, etc.
You can implement some of the functions yourself (Appendix D), but never
create variables with a name like this.
-
__*
-
Used in class-private name mangling; you won't really need this, as it's
a fairly advanced concept, but you can find out more about it at http://www.python.org/.
Builtin Names
The __builtin__ module contains a minimum set of function names
that you should never try to use, either as function names or variable
names. You can redefine them, but you won't like the results.
Here's the full list. Find out more at the usual place, http://www.python.org/.
__import__()
abs()
apply()
buffer()
callable()
chr()
cmp()
coerce()
compile()
complex()
delattr()
dir()
divmod()
eval()
execfile()
filter()
float()
getattr()
globals()
hasattr()
hash()
hex()
id()
intern()
int()
isinstance()
issubclass()
len()
list()
locals()
long()
map()
max()
min()
oct()
open()
ord()
pow()
range()
raw_input()
reduce()
reload()
repr()
round()
setattr()
slice()
str()
tuple()
type()
vars()
xrange()