Online Book Reader

Home Category

Learning Python - Mark Lutz [148]

By Root 1482 0
are your main interface to external files in a Python script, there are additional file-like tools in the Python toolset. Also available, to name a few, are:

Standard streams

Preopened file objects in the sys module, such as sys.stdout (see Print Operations)

Descriptor files in the os module

Integer file handles that support lower-level tools such as file locking

Sockets, pipes, and FIFOs

File-like objects used to synchronize processes or communicate over networks

Access-by-key files known as “shelves”

Used to store unaltered Python objects directly, by key (used in Chapter 27)

Shell command streams

Tools such as os.popen and subprocess.Popen that support spawning shell commands and reading and writing to their standard streams

The third-party open source domain offers even more file-like tools, including support for communicating with serial ports in the PySerial extension and interactive programs in the pexpect system. See more advanced Python texts and the Web at large for additional information on file-like tools.

* * *

Note


Version skew note: In Python 2.5 and earlier, the built-in name open is essentially a synonym for the name file, and files may technically be opened by calling either open or file (though open is generally preferred for opening). In Python 3.0, the name file is no longer available, because of its redundancy with open.

Python 2.6 users may also use the name file as the file object type, in order to customize files with object-oriented programming (described later in this book). In Python 3.0, files have changed radically. The classes used to implement file objects live in the standard library module io. See this module’s documentation or code for the classes it makes available for customization, and run a type(F) call on open files F for hints.

* * *

Type Categories Revisited

Now that we’ve seen all of Python’s core built-in types in action, let’s wrap up our object types tour by reviewing some of the properties they share. Table 9-3 classifies all the major types we’ve seen so far according to the type categories introduced earlier. Here are some points to remember:

Objects share operations according to their category; for instance, strings, lists, and tuples all share sequence operations such as concatenation, length, and indexing.

Only mutable objects (lists, dictionaries, and sets) may be changed in-place; you cannot change numbers, strings, or tuples in-place.

Files export only methods, so mutability doesn’t really apply to them—their state may be changed when they are processed, but this isn’t quite the same as Python core type mutability constraints.

“Numbers” in Table 9-3 includes all number types: integer (and the distinct long integer in 2.6), floating-point, complex, decimal, and fraction.

“Strings” in Table 9-3 includes str, as well as bytes in 3.0 and unicode in 2.6; the bytearray string type in 3.0 is mutable.

Sets are something like the keys of a valueless dictionary, but they don’t map to values and are not ordered, so sets are neither a mapping nor a sequence type; frozenset is an immutable variant of set.

In addition to type category operations, as of Python 2.6 and 3.0 all the types in Table 9-3 have callable methods, which are generally specific to their type.

Table 9-3. Object classifications

Object type

Category

Mutable?

Numbers (all)

Numeric

No

Strings

Sequence

No

Lists

Sequence

Yes

Dictionaries

Mapping

Yes

Tuples

Sequence

No

Files

Extension

N/A

Sets

Set

Yes

frozenset

Set

No

bytearray (3.0)

Sequence

Yes

* * *

Why You Will Care: Operator Overloading


In Part VI of this book, we’ll see that objects we implement with classes can pick and choose from these categories arbitrarily. For instance, if we want to provide a new kind of specialized sequence object that is consistent with built-in sequences, we can code a class that overloads things like indexing and concatenation:

class MySequence:

def __getitem__(self,

Return Main Page Previous Page Next Page

®Online Book Reader