12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Naming conventionsBesides these rules, there is also a set of naming conventions—rules that are notrequired, but are followed in normal practice. For instance, because names with twoleading and trailing underscores (e.g., _ _name_ _) generally have special meaning tothe <strong>Python</strong> interpreter, you should avoid this pattern for your own names. Here is alist of the conventions <strong>Python</strong> follows:• Names that begin with a single underscore (_X) are not imported by a frommodule import * statement (described in Chapter 19).• Names that have two leading and trailing underscores (__X__) are systemdefinednames 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 (described in Chapter 26).• The name that is just a single underscore (_) retains the result of the last expressionwhen working interactively.In addition to these <strong>Python</strong> interpreter conventions, there are various other conventionsthat <strong>Python</strong> programmers usually follow as well. For instance, later in thebook, we’ll see that class names commonly start with an uppercase letter, and modulenames with a lowercase letter, and that the name self, though not reserved, usuallyhas a special role in classes. In Part IV, we’ll also study another, larger categoryof names known as the built-ins, which are predefined, but not reserved (and so canbe reassigned: open = 42 works, though sometimes you might wish it didn’t!).Names have no type, but objects doThis is mostly review, but remember that it’s crucial to keep <strong>Python</strong>’s distinctionbetween names and objects clear. As described in “The Dynamic Typing Interlude”in Chapter 6, objects have a type (e.g., integer, list), and may be mutable or not.Names (a.k.a. variables), on the other hand, are always just references to objects;they have no notion of mutability, and have no associated type information, apartfrom the type of the object they happen to reference at a given point in time.It’s OK to assign the same name to different kinds of objects at different times:>>> x = 0 # x bound to an integer object>>> x = "Hello" # Now it's a string>>> x = [1, 2, 3] # And now it's a listIn later examples, you’ll see that this generic nature of names can be a decidedadvantage in <strong>Python</strong> programming. * In Part IV, you’ll learn that names also live insomething called a scope, which defines where they can be used; the place where youassign a name determines where it is visible.* If you’ve used C++, you may be interested to know that there is no notion of C++’s const declaration in<strong>Python</strong>; certain objects may be immutable, but names can always be assigned. <strong>Python</strong> also has ways to hidenames in classes and modules, but they’re not the same as C++’s declarations.Assignment Statements | 227

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!