Built-In Types


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 >>>


Table 1.1. Common Built-In Python Types

Type Category

Type Name

Description

None

types.NoneType

None object (null object)

Numbers

bool

Boolean True or False

 

int

Integer

 

long

Long integer

 

float

Floating point

 

complex

Complex number

Set

set

Mutable set

 

frozenset

Immutable set

Sequences

str

Character string

 

unicode

Unicode character string

 

basestring

Base type of all strings

 

list

List

 

tuple

Tuple

 

xrange

Immutable sequence

Mapping

dict

Dictionary

Files

file

File

Callable

type

Type for all built-ins

 

object

Parent of all types and classes

 

types.BuiltinFunctionType

Built-in function

 

types.BuiltinMethodType

Built-in method

 

types.FunctionType

User-defined function

 

types.InstanceType

Class instance

 

types.MethodType

Bound method

 

types.UnboundedMethodType

Unbound method

Modules

types.ModuleType

Module

Classes

object

Parent of all classes

Type

type

Type for all built-ins


Note

The type module must be imported to use any of the type objects such as type and types.ModuleType.


None

The 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.

Numbers

The 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.

Set

The 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.


Sequences

There 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.

Mapping

The 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.

Files

The 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.

Callable

Objects 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.


Modules

The 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




Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net