< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204172038188098235200064124

Other Built-in Types

  • Type

  • None

  • File

  • Function

  • Module

  • Class

  • Class Instance

  • Method

These are some of the other types you will interact with as you develop as a Python programmer. We will also cover these in Chapters 9, 11, 12, and 13 with the exception of the Type and None types, which we will discuss here.

Types and the type() Built-in Function

It may seem unusual perhaps, to regard types themselves as objects since we are attempting to just describe all of Python's types to you in this chapter. However, if you keep in mind that an object's set of inherent behaviors and characteristics (such as supported operators and built-in methods) must be defined somewhere, an object's type is a logical place for this information. The amount of information necessary to describe a type cannot fit into a single string; therefore types cannot simply be strings, nor should this information be stored with the data, so we are back to types as objects.

We will formally introduce the type() built-in function. The syntax is as follows:

						
type(object)

					

The type() built-in function takes object and returns its type. The return object is a type object.

						
>>> type(4)               #int type
<type 'int'>
>>>
>>> type('Hello World!')  #string type
<type 'string'>
>>>
>>> type(type(4))         #type type
<type 'type'>

					

In the examples above, we take an integer and a string and obtain their types using the type() built-in function; in order to also verify that types themselves are types, we call type() on the output of a type() call.

Note the interesting output from the type() function. It does not look like a typical Python data type, i.e., a number or string, but is something enclosed by greater-than and less-than signs. This syntax is generally a clue that what you are looking at is an object. Objects may implement a printable string representation; however, this is not always the case. In these scenarios where there is no easy way to "display" an object, Python "pretty-prints" a string representation of the object. The format is usually of the form: <object_something_or_another>. Any object displayed in this manner generally gives the object type, an object ID or location, or other pertinent information.

None

Python has a special type known as the Null object. It has only one value, None. The type of None is also None. It does not have any operators or built-in functions. If you are familiar with C, the closest analogy to the None type is void, while the None value is similar to the C value of NULL. (Other similar objects and values include Perl's undef and Java's Void type and null value.) None has no attributes and always evaluates to having a Boolean false value.


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.