The built-in types that you will most frequently use in Python can be grouped into the categories listed in Table 1.1 . The Type Name column shows the name that is associated with each built-in object type and can be used to determine whether an object is of a specific type using the isinstance(object, typename) function, as follows: >>> s = "A Simple String" >>> print isinstance(s, basestring) True >>> print isinstance(s, dict) False >>>
Note The type module must be imported to use any of the type objects such as type and types.ModuleType. NoneThe none type equates to a null object that has no value. The none type is the only object in Python that can be a null object. The syntax to use the none type in programs is simply None. NumbersThe numeric types in Python are very straightforward. The bool type has two possible values: true or False. The int type internally stores whole numbers up to 32 bits. The long type can store numbers in a range that is limited only by the available memory of the machine. The float type uses the native double-precision to store floating-point numbers up to 64 bits. The complex type stores values as a pair of floating-point numbers. The individual values are accessible using the z.real and z.imag attributes of the complex object. SetThe set type represents an unordered collection of unique items. There are two basic types of sets: mutable and immutable. Mutable sets can be modified (items can be added or removed). Immutable sets cannot be changed after they are created. Note All items that are placed in a set must be of immutable type. Therefore, sets cannot contain items such as lists or dictionaries. However, they can include items such as strings and tuples. SequencesThere are several sequence types in Python. Sequences are ordered and can be indexed by non-negative integers. Sequences are easily manipulated and can be made up of almost any Python object. The two most common types of sequences by far are the string and list types. Chapter 2, "Manipulating Strings," discusses creating and using the string type. Chapter 3, "Managing Data Types," discusses the most common types of sequences and how to create and manipulate them. MappingThe mapping type represents two collections of objects. The first collection is a set of key objects that index the second collection that contains a set of value objects. Each key object indexes a specific value object in the correlating set. The key object must be of an immutable type. The value object can be almost any Python object. The dictionary is the only mapping type currently built in to Python. Chapter 3 discusses dictionaries and how to create and manipulate them. FilesThe file type is a Python object that represents an open file. Objects of the file type can be used to read and write data to and from the filesystem. Chapter 4, "Managing Files," discusses file type objects and includes some of the most common Python phrases to utilize them. CallableObjects of the callable type support Python's function call operation, meaning that they can be called as a function of the program. Several objects fall into the callable type. The most common are the functions built in to the Python language, user-defined functions, classes, and method instances. Note Classes are considered callable because the class is called to create a new instance of the class. Once a new instance of a class has been called, the method instances of the class become callable also. ModulesThe module type represents Python modules that have been loaded by the import statement. The import statement creates a module type object with the same name as the Python module; then, all objects within the module are added to the __dict__ attribute of the newly created module type object. Objects from the module can be accessed directly using the dot syntax because it is translated into a dictionary lookup. This way, you can use module.object instead of accessing an attribute using module.__dict__("object") to access objects from the module. For example, the math module has the numeric object pi; the following code loads the math module and accesses the pi object: >>> import math >>> print math.pi 3.14159265359 |