home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Previous Section Next Section

8.1 Built-in Types

This section documents Python's core built-in types, like int, float, and dict. Note that prior to Python 2.2, these names referred to factory functions for creating objects of these types. As of Python 2.2, however, they refer to actual type objects. Since you can call type objects just as if they were functions, this change does not break existing programs.

classmethod Python 2.2 and later

classmethod(function)

Creates and returns a class method object. In practice, you call this built-in type only within a class body. See Section 5.2.2.2.

complex

complex(real,imag=0)

Converts any number, or a suitable string, to a complex number. imag may be present only when real is a number, and is the imaginary part of the resulting complex number.

dict Python 2.2 and later

dict(x={  })

Returns a new dictionary object with the same items as argument x. When x is a dictionary, dict(x) returns a copy of x, like x.copy( ) does. Alternatively, x can be a sequence of pairs, that is, a sequence whose items are sequences with two items each. In this case, dict(x) returns a dictionary whose keys are the first items of each pair in x, while the corresponding values are the corresponding second items. In other words, when x is a sequence, c=dict(x) has the same effect as the following:

c = {  }
for key, value in x: c[key] = value
file, open

file(path,mode='r',bufsize=-1)
open(filename,mode='r',bufsize=-1)

Opens or creates a file and returns a new file object. In Python 2.2 and later, open is a synonym for the built-in type file. In Python 2.1 and earlier, open was a built-in function and file was not a built-in name at all. See Section 10.3.

float

float(x)

Converts any number, or a suitable string, to a floating-point number.

int

int(x[,radix])

Converts any number, or a suitable string, to an int. When x is a number, int truncates toward 0, dropping any fractional part. radix may be present only when x is a string. radix is the conversion base, between 2 and 36, with 10 as the default. radix can be explicitly passed as 0: the base is then 8, 10, or 16, depending on the form of string x, just like for integer literals, as covered in Section 4.2.1.

list

list(seq=[  ])

Returns a new list object with the same items as the iterable object seq, in the same order. When seq is a list, list(seq) returns a copy of seq, like seq[:] does.

long

long(x[,radix])

Converts any number, or a suitable string, to a long. The rules regarding the radix argument are exactly the same as for int.

object

object(*args,**kwds)

Creates and returns a new instance of the most fundamental type. Such direct instances of type object have no useful functionality so there is never a practical reason to create one, although Python does let you call object for regularity. object accepts and ignores any positional and named arguments.

property Python 2.2 and later

property(fget=None,fset=None,fdel=None,doc=None)

Creates and returns a property accessor. In practice, you call this built-in type only within a class body. See Section 5.2.4.1.

staticmethod Python 2.2 and later

staticmethod(function)

Creates and returns a static method object. In practice, you call this built-in type only within a class body. See Section 5.2.2.1.

str

str(obj)

Returns a concise and readable string representation of obj. If obj is a string, str returns obj. See also repr later in this chapter and _ _str_ _ in Chapter 5.

super Python 2.2 and later

super(cls,obj)

Returns a super object of object obj (which must be an instance of class cls or of a subclass of cls), suitable for calling superclass methods. In practice, you call this built-in type only within a method's code. See Section 5.2.5.2.

tuple

tuple(seq)

Returns a tuple with the same items as the iterable object seq, in the same order. When seq is a tuple, tuple returns seq itself, like seq[:] does.

type

type(obj)

Returns the type object that represents the type of obj (i.e., the most-derived type object of which obj is an instance). All classic instance objects have the same type (InstanceType), even when they are instances of different classes; use isinstance (covered later in this chapter) to check whether an instance belongs to a particular class. In the new-style object model, however, type(x) is x._ _class_ _ for any x.

Checking type(x) for equality or identity to some other type object is known as type-checking. Type-checking is rarely appropriate in production Python code because it interferes with polymorphism. The normal idiom in Python is to try to use x as if it were of the type you expect, handling any problems with a try/except statement, as discussed in Chapter 6. When you must type-check, typically for debugging purposes, use isinstance instead. isinstance(x,atype) is a somewhat lesser evil than type(x) is atype, since at least it accepts an x that is an instance of any subclass of atype, not just a direct instance of atype itself.

unicode

unicode(string[,codec[,errors]])

Returns the Unicode string object obtained by decoding string. codec names the codec to use. If codec is missing, unicode uses the default codec (generally 'ascii'). errors, if present, is a string that specifies how to handle decoding errors. See also Section 9.6 in Chapter 9, particularly for information about codecs and errors, and _ _unicode_ _ in Chapter 5.

    Previous Section Next Section