< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204172037206197220143250153

Identifiers

Identifiers are the set of valid strings which are allowed as names in a computer language. From this all-encompassing list, we segregate out those which are keywords, names that form a construct of the language. Such identifiers are reserved words which may not be used for any other purpose, or else a syntax error (SyntaxError exception) will occur.

Python also has an additional set of identifiers known as built-ins, and although they are not reserved words, use of these special names is not recommended. (Also see Section 3.3.3.)

Valid Python Identifiers

The rules for Python identifier strings are not unlike most other high-level programming languages:

  • First character must be letter or underscore ( _ )

  • Any additional characters can be alphanumeric or underscore

  • Case-sensitive

No identifiers can begin with a number, and no symbols other than the underscore are ever allowed. The easiest way to deal with underscores is to consider them as alphabetic characters. Case-sensitivity means that identifier foo is different from Foo, and both of those are different from FOO.

Keywords

Python currently has twenty-eight keywords. They are listed in Table 3.1.

Generally, the keywords in any language should remain relatively stable, but should things ever change (as Python is a growing and evolving language), a list of keywords as well as an iskeyword() function are available in the keyword module.

Table 3.1. Python Keywords
and elif global or
assert else if pass
break except import print
class exec in raise
continue finally is return
def for lambda try
del from not while

For compatibility reasons, observe that the assert keyword is new as of Python 1.5, and the access keyword was obsolete beginning with 1.4.

Built-ins

In addition to keywords, Python has a set of "built-in" names which are either set and/or used by the interpreter that are available at any level of Python code. Although not keywords, built-ins should be treated as "reserved for the system" and not used for any other purpose. However, some circumstances may call for overriding (a.k.a. redefining, replacing) them. Python does not support overloading of identifiers, so only one name "binding" may exist at any given time.

Special Underscore Identifiers

Python designates (even more) special variables with underscores both prefixed and suffixed. We will also discover later that some are quite useful to the programmer while others are unknown or useless. Here is a summary of the special underscore usage in Python:

  • _xxx do not import with 'from module import *'

  • _xxx__ system-defined name

  • _xxx request private name mangling in classes

NOTE

Because of the underscore usage in Python system, interpreter, and built-in identifiers, we recommend that the programmer avoid the use of beginning variable names with the underscore.



Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.