Online Book Reader

Home Category

Learning Python - Mark Lutz [172]

By Root 1715 0
L

[1, 2, 3, 4, 5, 6, 7, 8]

In both cases, concatenation is less prone to the side effects of shared object references but will generally run slower than the in-place equivalent. Concatenation operations must create a new object, copy in the list on the left, and then copy in the list on the right. By contrast, in-place method calls simply add items at the end of a memory block.

When we use augmented assignment to extend a list, we can forget these details—for example, Python automatically calls the quicker extend method instead of using the slower concatenation operation implied by +:

>>> L += [9, 10] # Mapped to L.extend([9, 10])

>>> L

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Augmented assignment and shared references

This behavior is usually what we want, but notice that it implies that the += is an in-place change for lists; thus, it is not exactly like + concatenation, which always makes a new object. As for all shared reference cases, this difference might matter if other names reference the object being changed:

>>> L = [1, 2]

>>> M = L # L and M reference the same object

>>> L = L + [3, 4] # Concatenation makes a new object

>>> L, M # Changes L but not M

([1, 2, 3, 4], [1, 2])

>>> L = [1, 2]

>>> M = L

>>> L += [3, 4] # But += really means extend

>>> L, M # M sees the in-place change too!

([1, 2, 3, 4], [1, 2, 3, 4])

This only matters for mutables like lists and dictionaries, and it is a fairly obscure case (at least, until it impacts your code!). As always, make copies of your mutable objects if you need to break the shared reference structure.

Variable Name Rules

Now that we’ve explored assignment statements, it’s time to get more formal about the use of variable names. In Python, names come into existence when you assign values to them, but there are a few rules to follow when picking names for things in your programs:

Syntax: (underscore or letter) + (any number of letters, digits, or underscores)

Variable names must start with an underscore or letter, which can be followed by any number of letters, digits, or underscores. _spam, spam, and Spam_1 are legal names, but 1_Spam, spam$, and @#! are not.

Case matters: SPAM is not the same as spam

Python always pays attention to case in programs, both in names you create and in reserved words. For instance, the names X and x refer to two different variables. For portability, case also matters in the names of imported module files, even on platforms where the filesystems are case-insensitive.

Reserved words are off-limits

Names you define cannot be the same as words that mean special things in the Python language. For instance, if you try to use a variable name like class, Python will raise a syntax error, but klass and Class work fine. Table 11-3 lists the words that are currently reserved (and hence off-limits for names of your own) in Python.

Table 11-3. Python 3.0 reserved words

False

class

finally

is

return

None

continue

for

lambda

try

True

def

from

nonlocal

while

and

del

global

not

with

as

elif

if

or

yield

assert

else

import

pass

break

except

in

raise

Table 11-3 is specific to Python 3.0. In Python 2.6, the set of reserved words differs slightly:

print is a reserved word, because printing is a statement, not a built-in (more on this later in this chapter).

exec is a reserved word, because it is a statement, not a built-in function.

nonlocal is not a reserved word because this statement is not available.

In older Pythons the story is also more or less the same, with a few variations:

with and as were not reserved until 2.6, when context managers were officially enabled.

yield was not reserved until Python 2.3, when generator functions were enabled.

yield morphed from statement to expression in 2.5, but it’s still a reserved word, not a built-in function.

As you can see, most of Python’s reserved words are all lowercase. They are also all truly reserved—unlike names in the built-in scope that

Return Main Page Previous Page Next Page

®Online Book Reader