See All Titles |
![]() ![]() IdentifiersIdentifiers 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 IdentifiersThe rules for Python identifier strings are not unlike most other high-level programming languages:
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. KeywordsPython 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.
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-insIn 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 IdentifiersPython 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:
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.
|
© 2002, O'Reilly & Associates, Inc. |