Naming conventions

Besides these rules, there is also a set of naming conventions—rules that are not required but are followed in normal practice. For instance, because names with two leading and trailing underscores (e.g.,_name_) generally have special meaning to the Python interpreter, you should avoid this pattern for your own names. Here is a list of the conventions Python follows:

• Names that begin with a single underscore (_X) are not imported by a from module import * statement (described in Chapter 22).

• Names that have two leading and trailing underscores (__X__ ) are system-defined names that have special meaning to the interpreter.

• Names that begin with two underscores and do not end with two more (__X) are localized ("mangled") to enclosing classes (see the discussion of pseudoprivate attributes in Chapter 30).

• The name that is just a single underscore (_) retains the result of the last expression when working interactively.

In addition to these Python interpreter conventions, there are various other conventions that Python programmers usually follow. For instance, later in the book we'll see that class names commonly start with an uppercase letter and module names with a lowercase letter, and that the name self, though not reserved, usually has a special role in classes. In Chapter 17 we'll also study another, larger category of names known as the built-ins, which are predefined but not reserved (and so can be reassigned: open = 42 works, though sometimes you might wish it didn't!).

0 0

Post a comment