Type Specific Operations

Python's lists are related to arrays in other languages, but they tend to be more powerful. For one thing, they have no fixed type constraint the list we just looked at, for example, contains three objects of completely different types (an integer, a string, and a floating-point number). Further, lists have no fixed size. That is, they can grow and shrink on demand, in response to list-specific operations > > > L.append('NI') Growing add object at end of list > > > L.pop(2)...

A

Abs function, 125 absolute imports, 570 abstract superclasses, 690-693 example, 742 Python 2.6 and 3.0, 692 accessor functions, 417 ActivePython, 1090 annotation information, 472 anonymous functions, 474 anydbm module (Python 2.6), 670 append method, 87, 203, 388 apply built-in (Python 2.6), 449 arbitrary arguments examples apply built-in (Python 2.6), 449 applying functions generically, 448 collecting arguments, 446 unpacking arguments, 447 arguments, 435 mutable argument changes, avoiding,...

Basic Metaclass

Perhaps the simplest metaclass you can code is simply a subclass of type with a _new_method that creates the class object by running the default version in type. A metaclass__new_like this is run by the_call_method inherited from type it typically performs whatever customization is required and calls the type superclass's _new_method to create and return the new class object def _new_(meta, classname, supers, classdict) return type._new_(meta, classname, supers, classdict) This metaclass...

Few Special Cases

Here's what a continuation line looks like using the open syntactic pairs rule. Delimited constructs, such as lists in square brackets, can span across any number of lines L Good, Bad, Ugly Open pairs may span lines This also works for anything in parentheses (expressions, function arguments, function headers, tuples, and generator expressions), as well as anything in curly braces (dictionaries and, in 3.0, set literals and set and dictionary comprehensions). Some of these are tools we'll study...

First Example

Let's turn to a real example to show how these ideas work in practice. To begin, let's define a class named FirstClass by running a Python class statement interactively > > > class FirstClass Define a class object def setdata(self, value) Define class methods self.data value self is the instance print(self.data) self.data per instance We're working interactively here, but typically, such a statement would be run when the module file it is coded in is imported. Like functions created with...

First Script

Open your favorite text editor (e.g., vi, Notepad, or the IDLE editor), and type the following statements into a new text file named script1.py import sys Load a library module print(2 ** 100) Raise 2 to a power print(x * 8) String repetition This file is our first official Python script (not counting the two-liner in Chapter 2). You shouldn't worry too much about this file's code, but as a brief description, this file Imports a Python module (libraries of additional tools),...

Generic Display Tool

We can put these interfaces to work in a superclass that displays accurate class names and formats all attributes of an instance of any class. Open a new file in your text editor to code the following it's a new, independent module named classtools.py that implements just such a class. Because its_str_print overload uses generic introspection tools, it will work on any instance, regardless of its attributes set. And because this is a class, it automatically becomes a general formatting tool...

Python QA Session

If you've bought this book, you may already know what Python is and why it's an important tool to learn. If you don't, you probably won't be sold on Python until you've learned the language by reading the rest of this book and have done a project or two. But before we jump into details, the first few pages of this book will briefly introduce some of the main reasons behind Python's popularity. To begin sculpting a definition of Python, this chapter takes the form of a question-and-answer...

Second Example

To illustrate the role of inheritance, this next example builds on the previous one. First, we'll define a new class, SecondClass, that inherits all of FirstClass's names and provides one of its own > > > class SecondClass(FirstClass) Inherits setdata def display(self) Changes display print('Current value s' self.data) SecondClass defines the display method to print with a different format. By defining an attribute with the same name as an attribute in FirstClass, SecondClass effectively...

Second Example Intersecting Sequences

Let's look at a second function example that does something a bit more useful than multiplying arguments and further illustrates function basics. In Chapter 13, we coded a for loop that collected items held in common in two strings. We noted there that the code wasn't as useful as it could be because it was set up to work only on specific variables and could not be rerun later. Of course, we could copy the code and paste it into each place where it needs to be run, but this solution is neither...

Simple Interactive Loop

Suppose you're asked to write a Python program that interacts with a user in a console window. Maybe you're accepting inputs to send to a database, or reading numbers to be used in a calculation. Regardless of the purpose, you need to code a loop that reads one or more inputs from a user typing on a keyboard, and prints back a result for each. In other words, you need to write a classic read evaluate print loop program. In Python, typical boilerplate code for such an interactive loop might look...

Tale of Three Systems

The only time package imports are actually required is to resolve ambiguities that may arise when multiple programs with same-named files are installed on a single machine. This is something of an install issue, but it can also become a concern in general practice. Let's turn to a hypothetical scenario to illustrate. Suppose that a programmer develops a Python program that contains a file called utilities.py for common utility code and a top-level file named main.py that users launch to start...

Tale of Two ifs

Before we delve into the details of any of the concrete statements in Table 10-1, I want to begin our look at Python statement syntax by showing you what you are not going to type in Python code so you can compare and contrast it with other syntax models you might have seen in the past. Consider the following if statement, coded in a C-like language This might be a statement in C, C++, Java, JavaScript, or Perl. Now, look at the equivalent statement in the Python language The first thing that...

Third Example

This time, we'll define a subclass of SecondClass that implements three specially named attributes that Python will call automatically _init_is run when a new instance object is created (self is the new ThirdClass _add_is run when a ThirdClass instance appears in a + expression. _str_is run when an object is printed (technically, when it's converted to its print string by the str built-in function or its Python internals equivalent). Our new subclass also defines a...

A useful convenience

Keep in mind that extended sequence unpacking assignment is just a convenience. We can usually achieve the same effects with explicit indexing and slicing (and in fact must in Python 2.X), but extended unpacking is simpler to code. The common first, rest splitting coding pattern, for example, can be coded either way, but slicing involves extra work > > > a, *b seq First, rest > > > a, b seq 0 , seq l First, rest traditional The also common rest, last splitting pattern can similarly...

About the Programs in This Book

This fourth edition of this book, and all the program examples in it, is based on Python version 3.0. In addition, most of its examples run under Python 2.6, as described in the text, and notes for Python 2.6 readers are mixed in along the way. Because this text focuses on the core language, however, you can be fairly sure that most of what it has to say won't change very much in future releases of Python. Most of this book applies to earlier Python versions, too, except when it does not...

About This Fourth Edition

This fourth edition of this book has changed in three ways. This edition Covers both Python 3.0 and Python 2.6 it emphasizes 3.0, but notes differences in 2.6 Includes a set of new chapters mainly targeted at advanced core-language topics Reorganizes some existing material and expands it with new examples for clarity As I write this edition in 2009, Python comes in two flavors version 3.0 is an emerging and incompatible mutation of the language, and 2.6 retains backward compatibility with the...

Abstract Superclasses

Notice how the Provider class in the prior example works. When we call the delegate method through a Provider instance, two independent inheritance searches occur 1. On the initial x.delegate call, Python finds the delegate method in Super by searching the Provider instance and above. The instance x is passed into the method's self argument as usual. 2. Inside the Super.delegate method, self.action invokes a new, independent inheritance search of self and above. Because self references a...

Acknowledgments

As I write this fourth edition of this book in 2009, I can't help but be in a sort of mission accomplished state of mind. I have now been using and promoting Python for 17 years, and have been teaching it for 12 years. Despite the passage of time and events, I am still constantly amazed at how successful Python has been over the years. It has grown in ways that most of us could not possibly have imagined in 1992. So, at the risk of sounding like a hopelessly self-absorbed author, you'll have to...

Adding Decorator Arguments

The timer decorator of the prior section works, but it would be nice if it was more configurable providing an output label and turning trace messages on and off, for instance, might be useful in a general-purpose tool like this. Decorator arguments come in handy here when they're coded properly, we can use them to specify configuration options that can vary for each decorated function. A label, for instance, might be added as follows def timer(label '') def decorator(func) def onCall(*args)...

Adding Keys Attributes and Offsets

Like formatting expressions, format calls can become more complex to support more advanced usage. For instance, format strings can name object attributes and dictionary keys as in normal Python syntax, square brackets name dictionary keys and dots denote object attributes of an item referenced by position or keyword. The first of the following examples indexes a dictionary on the key spam and then fetches the attribute platform from the already imported sys module object. The second does the...

Adding Specific Formatting

Another similarity with expressions is that more specific layouts can be achieved by adding extra syntax in the format string. For the formatting method, we use a colon after the substitution target's identification, followed by a format specifier that can name the field size, justification, and a specific type code. Here's the formal structure of what can appear as a substitution target in a format string fieldname is a number or keyword naming an argument, followed by optional .name attribute...

Adding Tests and Nested Loops filter

List comprehensions are even more general than shown so far. For instance, as we learned in Chapter 14, you can code an if clause after the for to add selection logic. List comprehensions with if clauses can be thought of as analogous to the filter built-in discussed in the prior chapter they skip sequence items for which the if clause is not true. To demonstrate, here are both schemes picking up even numbers from 0 to 4 like the map list comprehension alternative of the prior section, the...

Advanced IDLE Tools

Besides the basic edit and run functions, IDLE provides more advanced features, including a point-and-click program debugger and an object browser. The IDLE debugger is enabled via the Debug menu and the object browser via the File menu. The browser allows you to navigate through the module search path to files and objects in files clicking on a file or object opens the corresponding source in a text edit window. IDLE debugging is initiated by selecting the Debug Debugger menu option in the...

Advanced Module Selection Concepts

Normally, imports work as described in this section they find and load files on your machine. However, it is possible to redefine much of what an import operation does in Python, using what are known as import hooks. These hooks can be used to make imports do various useful things, such as loading files from archives, performing decryption, and so on. In fact, Python itself makes use of these hooks to enable files to be directly imported from ZIP archives archived files are automatically...

Advanced Module Topics

This chapter concludes this part of the book with a collection of more advanced module-related topics data hiding, the_future_ module, the_name_variable, sys.path changes, listing tools, running modules by name string, transitive reloads, and so on along with the standard set of gotchas and exercises related to what we've covered in this part of the book. Along the way, we'll build some larger and more useful tools than we have so far, that combine functions and modules. Like functions, modules...

Advanced String Formatting Expressions

For more advanced type-specific formatting, you can use any of the conversion type codes listed in Table 7-4 in formatting expressions they appear after the character in substitution targets. C programmers will recognize most of these because Python string formatting supports all the usual C printf format codes (but returns the result, instead of displaying it, like printf). Some of the format codes in the table provide alternative ways to format the same type for instance, e, f, and g provide...

Anonymous Functions lambda

Besides the def statement, Python also provides an expression form that generates function objects. Because of its similarity to a tool in the Lisp language, it's called lambda.* Like def, this expression creates a function to be called later, but it returns the function instead of assigning it to a name. This is why lambdas are sometimes known as anonymous (i.e., unnamed) functions. In practice, they are often used as a way to inline a function definition, or to defer execution of a piece of...

Application to for loops

Because the loop variable in the for loop statement can be any assignment target, extended sequence assignment works here too. We met the for loop iteration tool briefly in Part II and will study it formally in Chapter 13. In Python 3.0, extended assignments may show up after the word for, where a simple variable name is more commonly used for (a, *b, c) in (1, 2, 3, 4), (5, 6, 7, 8) When used in this context, on each iteration Python simply assigns the next tuple of values to the tuple of...

Argument Passing Basics

Earlier in this part of the book, I noted that arguments are passed by assignment. This has a few ramifications that aren't always obvious to beginners, which I'll expand on in this section. Here is a rundown of the key points in passing arguments to functions Arguments are passed by automatically assigning objects to local variable names. Function arguments references to (possibly) shared objects sent by the caller are just another instance of Python assignment at work. Because references are...

Arguments

Chapter 17 explored the details behind Python's scopes the places where variables are defined and looked up. As we learned, the place where a name is defined in our code determines much of its meaning. This chapter continues the function story by studying the concepts in Python argument passing the way that objects are sent to functions as inputs. As we'll see, arguments (a.k.a. parameters) are assigned to names in a function, but they have more to do with object references than with variable...

Arguments and Shared References

a is assigned to (references) passed object Changes local variable a only > > > b 88 > > > f(b) > > > print(b) a and b both reference same 88 initially > > > def changer(a, b) Arguments assigned references to objects a 2 Changes local name's value only b 0 'spam' Changes shared object in-place > > > L 1, 2 Caller > > > changer(X, L) Pass immutable and mutable objects > > > X, L X is unchanged, L is different In this code, the changer function assigns...

Assignment Creates References Not Copies

Because this is such a central concept, I'll mention it again you need to understand what's going on with shared references in your program. For instance, in the following example, the list object assigned to the name L is referenced from L and from inside the list assigned to the name M. Changing L in-place changes what M references, too > > > L 1, 2, 3 > > > M 'X', L, 'Y' Embed a reference to L > > > L l 0 Changes M too This effect usually becomes important only in larger...

Assignment Statements

We've been using the Python assignment statement for a while to assign objects to names. In its basic form, you write the target of an assignment on the left of an equals sign, and the object to be assigned on the right. The target on the left may be a name or object component, and the object on the right can be an arbitrary expression that computes an object. For the most part, assignments are straightforward, but here are a few properties to keep in mind Assignments create object references....

Assignments Expressions and Prints

Now that we've had a quick introduction to Python statement syntax, this chapter begins our in-depth tour of specific Python statements. We'll begin with the basics assignment statements, expression statements, and print operations. We've already seen all of these in action, but here we'll fill in important details we've skipped so far. Although they're fairly simple, as you'll see, there are optional variations for each of these statement types that will come in handy once you begin writing...

Attribute Names Object Namespaces

Qualified attribute names refer to attributes of specific objects and obey the rules for modules and classes. For class and instance objects, the reference rules are augmented to include the inheritance search procedure Creates or alters the attribute name X in the namespace of the object being qualified, and none other. Inheritance-tree climbing happens only on attribute reference, not on attribute assignment. For class-based objects, searches for the attribute name X in object, then in all...

Attribute Referencegetattrandsetattr

The _getattr_ method intercepts attribute qualifications. More specifically, it's called with the attribute name as a string whenever you try to qualify an instance with an undefined (nonexistent) attribute name. It is not called if Python can find the attribute using its inheritance tree search procedure. Because of its behavior,_getattr_is useful as a hook for responding to attribute requests in a generic fashion. For example > > > X empty() > > > X.age error text omitted...

Attribute Tree Construction

Figure 28-1 summarizes the way namespace trees are constructed and populated with names. Generally Instance attributes are generated by assignments to self attributes in methods. Class attributes are created by statements (assignments) in class statements. Superclass links are made by listing classes in parentheses in a class statement header. The net result is a tree of attribute namespaces that leads from an instance, to the class it was generated from, to all the superclasses listed in the...

Augmented assignment and shared references

This behavior is usually what we want, but notice that it implies that the + is an inplace 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 andM reference the same object > > > L L + 3, 4 Concatenation makes a new object > > > L, M Changes L but not M > > > L + 3, 4 But + really...

Augmented Assignments

Beginning with Python 2.0, the set of additional assignment statement formats listed in Table 11-2 became available. Known as augmented assignments, and borrowed from the C language, these formats are mostly just shorthand. They imply the combination of a binary expression and an assignment. For instance, the following two formats are now roughly equivalent Table 11-2. Augmented assignment statements X + Y X & Y X - Y X Y X * Y X A Y X Y X > > Y X Y X < < Y X ** Y X Y Augmented...

Augmenting Methods The Bad

Now, there are two ways we might code this Manager customization a good way and a bad way. Let's start with the bad way, since it might be a bit easier to understand. The bad way is to cut and paste the code of giveRaise in Person and modify it for Manager, like this def giveRaise(self, percent, bonus .10) self.pay int(self.pay * (1 + percent + bonus)) Bad cut-and-paste This works as advertised when we later call the giveRaise method of a Manager instance, it will run this custom version, which...

Augmenting Methods The Good

What we really want to do here is somehow augment the original giveRaise, instead of replacing it altogether. The good way to do that in Python is by calling to the original version directly, with augmented arguments, like this def giveRaise(self, percent, bonus .10) Person.giveRaise(self, percent + bonus) Good augment original This code leverages the fact that a class method can always be called either through an instance (the usual way, where Python sends the instance to the self argument...

Automatic stream redirection

This technique of redirecting printed text by assigning sys.stdout is commonly used in practice. One potential problem with the last section's code, though, is that there is no direct way to restore the original output stream should you need to switch back after printing to a file. Because sys.stdout is just a normal file object, you can always save it and restore it if needed C misc> c python30 python > > > import sys > > > temp sys.stdout Save for restoring later > > >...

Avoid mixing tabs and spaces New error checking in

One rule of thumb although you can use spaces or tabs to indent, it's usually not a good idea to mix the two within a block use one or the other. Technically, tabs count for enough spaces to move the current column number up to a multiple of 8, and your code will work if you mix tabs and spaces consistently. However, such code can be difficult to change. Worse, mixing tabs and spaces makes your code difficult to read tabs may look very different in the next programmer's editor than they do in...

Avoiding missingkey errors

Errors for nonexistent key fetches are common in sparse matrixes, but you probably won't want them to shut down your program. There are at least three ways to fill in a default value instead of getting such an error message you can test for keys ahead of time in if statements, use a try statement to catch and recover from the exception explicitly, or simply use the dictionary get method shown earlier to provide a default for keys that do not exist > > > if (2,3,6) in Matrix Check for key...

Avoiding Mutable Argument Changes

This behavior of in-place changes to mutable arguments isn't a bug it's simply the way argument passing works in Python. Arguments are passed to functions by reference (a.k.a. pointer) by default because that is what we normally want. It means we can pass large objects around our programs without making multiple copies along the way, and we can easily update these objects as we go. In fact, as we'll see in Part VI, Python's class model depends upon changing a passed-in self argument in-place,...

Basic Dictionary Operations

In normal operation, you create dictionaries with literals and store and access items by key with indexing python > > > D 'spam' 2, 'ham' 1, 'eggs' 3 Make a dictionary > > > D 'spam' Fetch a value by key > > > D Order is scrambled Here, the dictionary is assigned to the variable D the value of the key 'spam' is the integer 2, and so on. We use the same square bracket syntax to index dictionaries by key as we did to index lists by offset, but here it means access by key, not by...

Basic Examples

To demonstrate, let's look at a few simple examples of the if statement at work. All parts are optional, except the initial if test and its associated statements. Thus, in the simplest case, the other parts are omitted > > > if 1 Notice how the prompt changes to for continuation lines when typing interactively in the basic interface used here in IDLE, you'll simply drop down to an indented line instead (hit Backspace to back up). A blank line (which you can get by pressing Enter twice)...

Basic List Operations

Because they are sequences, lists support many of the same operations as strings. For example, lists respond to the + and * operators much like strings they mean concatenation and repetition here too, except that the result is a new list, not a string python > > > len( 1, 2, 3 ) Length > > > 1, 2, 3 + 4, 5, 6 Concatenation > > > 'Ni ' * 4 Repetition Although the + operator works the same for lists and strings, it's important to know that it expects the same sort of sequence on...

Basic Operations

Let's begin by interacting with the Python interpreter to illustrate the basic string operations listed earlier in Table 7-1. Strings can be concatenated using the + operator and repeated using the * operator python > > > len('abc') Length number of items > > > 'abc' + 'def' Concatenation a new string > > > 'Ni ' * 4 Repetition like Nil + Nil + Formally, adding two string objects creates a new string object, with the contents of its operands joined. Repetition is like adding a...

Basic Usage

The basic format of the with statement looks like this with expression as variable with-block The expression here is assumed to return an object that supports the context management protocol (more on this protocol in a moment). This object may also return a value that will be assigned to the name variable if the optional as clause is present. Note that the variable is not necessarily assigned the result of the expression the result of the expression is the object that supports the context...

Beware of Cyclic Data Structures

We actually encountered this concept in a prior exercise if a collection object contains a reference to itself, it's called a cyclic object. Python prints a whenever it detects a cycle in the object, rather than getting stuck in an infinite loop > > > L 'grail' Append reference to same object > > > L.append(L) Generates cycle in object Besides understanding that the three dots in square brackets represent a cycle in the object, this case is worth knowing about because it can lead to...

Block Delimiters Indentation Rules

Python detects block boundaries automatically, by line indentation that is, the empty space to the left of your code. All statements indented the same distance to the right belong to the same block of code. In other words, the statements within a block line up vertically, as in a column. The block ends when the end of the file or a lesser-indented line is encountered, and more deeply nested blocks are simply indented further to the right than the statements in the enclosing block. For instance,...

Block rule special case

As mentioned previously, statements in a nested block of code are normally associated by being indented the same amount to the right. As one special case here, the body of a compound statement can instead appear on the same line as the header in Python, after the colon This allows us to code single-line if statements, single-line loops, and so on. Here again, though, this will work only if the body of the compound statement itself does not contain any compound statements. That is, only simple...

Bonus Points

You can get can get bonus points here for changing these functions to compute the maximum, rather than minimum, values. This one's easy the first two versions only require changing < to > , and the third simply requires that we return tmp -l instead of tmp 0 . For an extra point, be sure to set the function name to max as well (though this part is strictly optional). It's also possible to generalize a single function to compute either a minimum or a maximum value, by evaluating comparison...

Book Updates

Improvements happen (and so do misAHAHAH typos). Updates, supplements, and corrections for this book will be maintained (or referenced) on the Web at one of the following sites (O'Reilly's web page for the book) http www.rmi.net lutz (the author's site) (the author's web page for the book) The last of these three URLs points to a web page for this book where I will post updates, but be sure to search the Web if this link becomes invalid. If I could become more clairvoyant, I would, but the Web...

Booleans

Flags can be initialized more clearly with flag False. We'll discuss these statements further in Part III. Again, though, for all other practical purposes, you can treat True and False as though they are predefined variables set to integer 1 and 0. Most programmers used to preassign True and False to 1 and 0 anyway the bool type simply makes this standard. Its implementation can lead to curious results, though. Because True is just the integer 1 with a custom display format, True + 4 yields 5...

Bound Methods and Other Callable Objects

As mentioned earlier, bound methods can be processed as generic objects, just like simple functions they can be passed around a program arbitrarily. Moreover, because bound methods combine both a function and an instance in a single package, they can be treated like any other callable object and require no special syntax when invoked. The following, for example, stores four bound method objects in a list and calls them later with normal call expressions > > > x Number(2) Class instance...

Boundary cases

Although extended sequence unpacking is flexible, some boundary cases are worth noting. First, the starred name may match just a single item, but is always assigned a list > > > a, b, c, *d seq > > > print(a, b, c, d) Second, if there is nothing left to match the starred name, it is assigned an empty list, regardless of where it appears. In the following, a, b, c, and d have matched every item in the sequence, but Python assigns e an empty list instead of treating this as an error...

Builtin Exception Categories

The built-in class tree allows you to choose how specific or general your handlers will be. For example, the built-in exception ArithmeticError is a superclass for more specific exceptions such as OverflowError and ZeroDivisionError. By listing ArithmeticError in a try, you will catch any kind of numeric error raised by listing just OverflowError, you will intercept just that specific type of error, and no others. Similarly, because Exception is the superclass of all application-level...

Builtin Exception Classes

I didn't really pull the prior section's examples out of thin air. All built-in exceptions that Python itself may raise are predefined class objects. Moreover, they are organized into a shallow hierarchy with general superclass categories and specific subclass types, much like the exceptions class tree we developed earlier. In Python 3.0, all the familiar exceptions you've seen (e.g., SyntaxError) are really just predefined classes, available as built-in names in the module named builtins (in...

Builtin Numeric Tools

Besides the built-in number literals shown in Table 5-1, Python provides a set of tools for processing number objects +, -, *, , > > , **, & , etc. pow, abs, round, int, hex, bin, etc. Utility modules We'll meet all of these as we go along. Although numbers are primarily processed with expressions, built-ins, and modules, they also have a handful of type-specific methods today, which we'll meet in this chapter as well. Floating-point numbers, for example, have an as_integer_ratio method...

Call Expressionscall

The_call_method is called when your instance is called. No, this isn't a circular definition if defined, Python runs a_call_method for function call expressions applied to your instances, passing along whatever positional or keyword arguments were sent def _call_(self, *pargs, **kargs) Intercept instance calls print('Called ', pargs, kargs) Accept arbitrary arguments > > > C(1, 2, 3) C is a callable object More formally, all the argument-passing modes we explored in Chapter 18 are...

Calling Superclass Constructors

Methods are normally called through instances. Calls to methods through a class, though, do show up in a variety of special roles. One common scenario involves the constructor method. The __init__ method, like all attributes, is looked up by inheritance. This means that at construction time, Python locates and calls just one _init_. If subclass constructors need to guarantee that superclass construction-time logic runs, too, they generally must call the superclass's_init_method explicitly...

Calls

After the def has run, you can call (run) the function in your program by adding parentheses after the function's name. The parentheses may optionally contain one or more object arguments, to be passed (assigned) to the names in the function's header > > > times(2, 4) Arguments in parentheses This expression passes two arguments to times. As mentioned previously, arguments are passed by assignment, so in this case the name x in the function header is assigned the value 2, y is assigned...

Catching Too Little Use Class Based Categories

On the other hand, neither should handlers be too specific. When you list specific exceptions in a try, you catch only what you actually list. This isn't necessarily a bad thing, but if a system evolves to raise other exceptions in the future, you may need to go back and add them to exception lists elsewhere in your code. We saw this phenomenon at work in the prior chapter. For instance, the following handler is written to treat MyExceptl and MyExcept2 as normal cases and everything else as an...

Changes in Python

The way import operations in packages work has changed slightly in Python 3.0. This change applies only to imports within files located in the package directories we've been studying in this chapter imports in other files work as before. For imports in packages, though, Python 3.0 introduces two changes It modifies the module import search path semantics to skip the package's own directory by default. Imports check only other components of the search path. These are known as absolute imports....

Changes to Existing Material

In addition, some material from the prior edition has been reorganized, or supplemented with new examples. Multiple inheritance, for instance, gets a new case study example that lists class trees in Chapter 30 new examples for generators that manually implement map and zip are provided in Chapter 20 static and class methods are illustrated by new code in Chapter 31 package relative imports are captured in action in Chapter 23 and the_contains_,__bool_, and_index__operator overloading methods...

Changing Dictionaries InPlace

Let's continue with our interactive session. Dictionaries, like lists, are mutable, so you can change, expand, and shrink them in-place without making new dictionaries simply assign a value to a key to change or create an entry. The del statement works here, too it deletes the entry associated with the key specified as an index. Notice also the nesting of a list inside a dictionary in this example (the value of the key 'ham'). All collection data types in Python can nest inside each other...

Changing Lists InPlace

Because lists are mutable, they support operations that change a list object in-place. That is, the operations in this section all modify the list object directly, without requiring that you make a new copy, as you had to for strings. Because Python deals only in object references, this distinction between changing an object in-place and creating a new object matters as discussed in Chapter 6, if you change an object in-place, you might impact more than one reference to it at the same time.

Changing Lists range

Another common place where you may use the range and for combination is in loops that change a list as it is being traversed. Suppose, for example, that you need to add 1 to every item in a list. You can try this with a simple for loop, but the result probably won't be exactly what you want This doesn't quite work it changes the loop variable x, not the list L. The reason is somewhat subtle. Each time through the loop, x refers to the next integer already pulled out of the list. In the first...

Changing Mutable Class Attributes Can Have Side Effects

This gotcha is really an extension of the prior. Because class attributes are shared by all instances, if a class attribute references a mutable object, changing that object in-place from any instance impacts all instances at once > > > class C shared Class attribute def _init_(self) self.perobj Instance attribute > > > x C() Two instances > > > y C() Implicitly share class attrs > > > y.shared, y.perobj ( , ) > > > x.shared.append('spam') Impacts y s view too >...

Changing Strings

Remember the term immutable sequence The immutable part means that you can't change a string in-place (e.g., by assigning to an index) > > > S 'spam' > > > S 0 x So, how do you modify text information in Python To change a string, you need to build and assign a new string using tools such as concatenation and slicing, and then, if desired, assign the result back to the string's original name > > > S S + 'SPAM ' To change a string, make a new one > > > S S 4 + 'Burger' +...

Chapter Summary

And that concludes the hype portion of this book. In this chapter, we've explored some of the reasons that people pick Python for their programming tasks. We've also seen how it is applied and looked at a representative sample of who is using it today. My goal is to teach Python, though, not to sell it. The best way to judge a language is to see it in action, so the rest of this book focuses entirely on the language details we've glossed over here. The next two chapters begin our technical...

Character Encoding Schemes

Most programmers think of strings as series of characters used to represent textual data. The way characters are stored in a computer's memory can vary, though, depending on what sort of character set must be recorded. The ASCII standard was created in the U.S., and it defines many U.S. programmers' notion of text strings. ASCII defines character codes from 0 through 127 and allows each character to be stored in one 8-bit byte (only 7 bits of which are actually used). For example, the ASCII...

Class Blunders II Retaining Multiple Instances

Curiously, the decorator function in this example can almost be coded as a class instead of a function, with the proper operator overloading protocol. The following slightly simplified alternative works similarly because its_init_is triggered when the decorator is applied to the class, and its_call_is triggered when a subject class instance is created. Our objects are really instances of Tracer this time, and we essentially just trade an enclosing scope reference for an instance attribute here...

Class Coding Basics

Now that we've talked about OOP in the abstract, it's time to see how this translates to actual code. This chapter begins to fill in the syntax details behind the class model in Python. If you've never been exposed to OOP in the past, classes can seem somewhat complicated if taken in a single dose. To make class coding easier to absorb, we'll begin our detailed exploration of OOP by taking a first look at some basic classes in action in this chapter. We'll expand on the details introduced here...

Class Coding Details

If you haven't quite gotten all of Python OOP yet, don't worry now that we've had a quick tour, we're going to dig a bit deeper and study the concepts introduced earlier in further detail. In this and the following chapter, we'll take another look at class mechanics. Here, we're going to study classes, methods, and inheritance, formalizing and expanding on some of the coding ideas introduced in Chapter 26. Because the class is our last namespace tool, we'll summarize Python's namespace concepts...

Class Decorators and Metaclasses

Function decorators turned out to be so useful that Python 2.6 and 3.0 expanded the model, allowing decorators to be applied to classes as well as functions. In short, class decorators are similar to function decorators, but they are run at the end of a class statement to rebind a class name to a callable. As such, they can be used to either manage classes just after they are created, or insert a layer of wrapper logic to manage instances when they are later created. Symbolically, the code...

Class Interface Techniques

Extension is only one way to interface with a superclass. The file shown in this section, specialize.py, defines multiple classes that illustrate a variety of common techniques Defines a method function and a delegate that expects an action in a subclass. Inheritor Doesn't provide any new names, so it gets everything defined in Super. Replacer Overrides Super's method with a version of its own. Extender Customizes Super's method by overriding and calling back to run the default. Provider...

Class Objects Provide Default Behavior

When we run a class statement, we get a class object. Here's a rundown of the main properties of Python classes The class statement creates a class object and assigns it a name. Just like the function def statement, the Python class statement is an executable statement. When reached and run, it generates a new class object and assigns it to the name in the class header. Also, like defs, class statements typically run when the files they are coded in are first imported. Assignments inside class...

Class Statement Protocol

Subclassing the type class to customize it is really only half of the magic behind meta-classes. We still need to somehow route a class's creation to the metaclass, instead of the default type. To fully understand how this is arranged, we also need to know how class statements do their business. We've already learned that when Python reaches a class statement, it runs its nested block of code to create its attributes all the names assigned at the top level of the nested code block generate...

Class Based Exceptions

Strings were a simple way to define exceptions. As described earlier, however, classes have some added advantages that merit a quick look. Most prominently, they allow us to identify exception categories that are more flexible to use and maintain than simple strings. Moreover, classes naturally allow for attached exception details and support inheritance. Because they are the better approach, they are now required. Coding details aside, the chief difference between string and class exceptions...

Classes and Instances

Although they are technically two separate object types in the Python model, the classes and instances we put in these trees are almost identical each type's main purpose is to serve as another kind of namespace a package of variables, and a place where we can attach attributes. If classes and instances therefore sound like modules, they should however, the objects in class trees also have automatically searched links to other namespace objects, and classes correspond to statements, not entire...

Classes Are Attributes in Modules

Before we move on, remember that there's nothing magic about a class name. It's just a variable assigned to an object when the class statement runs, and the object can be referenced with any normal expression. For instance, if our FirstClass was coded in a module file instead of being typed interactively, we could import it and use its name normally in a class header line from modulename import FirstClass class SecondClass(FirstClass) def display(self) class SecondClass(modulename.FirstClass)...

Classes Are Customized by Inheritance

Besides serving as factories for generating multiple instance objects, classes also allow us to make changes by introducing new components (called subclasses), instead of changing existing components in-place. Instance objects generated from a class inherit the class's attributes. Python also allows classes to inherit from other classes, opening the door to coding hierarchies of classes that specialize behavior by redefining attributes in subclasses that appear lower in the hierarchy, we...

Classes Are Objects Generic Object Factories

Sometimes, class-based designs require objects to be created in response to conditions that can't be predicted when a program is written. The factory design pattern allows such a deferred approach. Due in large part to Python's flexibility, factories can take multiple forms, some of which don't seem special at all. Because classes are objects, it's easy to pass them around a program, store them in data structures, and so on. You can also pass classes to functions that generate arbitrary kinds...

Classes Can Intercept Python Operators

Let's move on to the third major difference between classes and modules operator overloading. In simple terms, operator overloading lets objects coded with classes intercept and respond to operations that work on built-in types addition, slicing, printing, qualification, and so on. It's mostly just an automatic dispatch mechanism expressions and other built-in operations route control to implementations in classes. Here, too, there is nothing similar in modules modules can implement function...

Classes Generate Multiple Instance Objects

To understand how the multiple objects idea works, you have to first understand that there are two kinds of objects in Python's OOP model class objects and instance objects. Class objects provide default behavior and serve as factories for instance objects. Instance objects are the real objects your programs process each is a namespace in its own right, but inherits (i.e., has automatic access to) names in the class from which it was created. Class objects come from statements, and instances...

Classes Versus Dictionaries

Although the simple classes of the prior section are meant to illustrate class model basics, the techniques they employ can also be used for real work. For example, Chapter 8 showed how to use dictionaries to record properties of entities in our programs. It turns out that classes can serve this role, too they package information like dictionaries, but can also bundle processing logic in the form of methods. For reference, here is the example for dictionary-based records we used earlier in the...

Classes Versus Modules

Let's wrap up this chapter by briefly comparing the topics of this book's last two parts modules and classes. Because they're both about namespaces, the distinction can be confusing. In short Are created by writing Python files or C extensions Classes Implement new objects Always live within a module Classes also support extra features that modules don't, such as operator overloading, multiple instance generation, and inheritance. Although both classes and modules are namespaces, you should be...

Clicking File Icons

On Windows, the Registry makes opening files with icon clicks easy. Python automatically registers itself to be the program that opens Python program files when they are clicked. Because of that, it is possible to launch the Python programs you write by simply clicking (or double-clicking) on their file icons with your mouse cursor. On non-Windows systems, you will probably be able to perform a similar trick, but the icons, file explorer, navigation schemes, and more may differ slightly. On...

Clicking Icons on Windows

To illustrate, let's keep using the script we wrote earlier, script1.py, repeated here to minimize page flipping import sys Load a library module print(2 ** 100) Raise 2 to a power print(x * 8) String repetition As we've seen, you can always run this file from a system command line C misc> c python30 python scriptl.py However, icon clicks allow you to run the file without any typing at all. If you find this file's icon for instance, by selecting Computer (or My Computer in XP) in your Start...

Closing Files and Server Connections

We encountered examples in this category in Chapter 33. As a summary, though, exception processing tools are also commonly used to ensure that system resources are finalized, regardless of whether an error occurs during processing or not. For example, some servers require connections to be closed in order to terminate a session. Similarly, output files may require close calls to flush their buffers to disk, and input files may consume file descriptors if not closed although file objects are...

Coding Alternatives

Interestingly, we can also use Python's if else ternary expression (described in Chapter 12) to save some code real-estate here. We can also generalize for any summable type (which is easier if we assume at least one item in the input, as we did in Chapter 18's minimum value example) and use Python 3.0's extended sequence assignment to make the first rest unpacking simpler (as covered in Chapter 11) def mysum(L) return 0 if not L else L 0 + mysum(L 1 ) Use ternary expression return L 0 if...

Coding Ascii Text

Let's step through some examples that demonstrate text coding basics. As we've seen, ASCII text is a simple type of Unicode, stored as a sequence of byte values that represent characters > > > ord('X') 'X' has binary value 88 in the default encoding > > > chr(88) 88 stands for character 'X' > > > S 'XYZ' A Unicode string of ASCII text > > > len(S) 3 characters long > > > ord(c) for c in S 3 bytes with integer ordinal values 88, 89, 90 Normal 7-bit ASCII text like...

Coding Class Decorators

So far we've been coding function decorators to manage function calls, but as we've seen, Python 2.6 and 3.0 extend decorators to work on classes too. As described earlier, while similar in concept to function decorators, class decorators are applied to classes instead they may be used either to manage classes themselves, or to intercept instance creation calls in order to manage instances. Also like function decorators, class decorators are really just optional syntactic sugar, though many...

Coding Constructors

Constructor takes 3 arguments self is the new instance object self.name name self.job job self.pay pay subject of the method call. They are two different variables, which happen to have the same name. By assigning the job local to the self.job attribute with self.job job, we save the passed-in job on the instance for later use. As usual in Python, where a name is assigned (or what object it is assigned to) determines what it means. Speaking of arguments, there's really nothing magical...

Coding Exceptions Classes

Let's look at an example to see how class exceptions translate to code. In the following file, classexc.py, we define a superclass called General and two subclasses called Specificl and Specific2. This example illustrates the notion of exception categories General is a category name, and its two subclasses are specific types of exceptions within the category. Handlers that catch General will also catch any subclasses of it, including Specific1 and Specific2 class General(Exception) pass class...

Coding Functions

Although it wasn't made very formal, we've already used some functions in earlier chapters. For instance, to make a file object, we called the built-in open function similarly, we used the len built-in function to ask for the number of items in a collection object. In this chapter, we will explore how to write new functions in Python. Functions we write behave the same way as the built-ins we've already seen they are called in expressions, are passed values, and return results. But writing new...