Section 4.3. Other Built-in Types


4.3. Other Built-in Types

  • Type

  • Null object (None)

  • File

  • Set/Frozenset

  • Function/Method

  • Module

  • Class

These are some of the other types you will interact with as you develop as a Python programmer. We will also cover all of these in other chapters of this book with the exception of the type and None types, which we will discuss here.

4.3.1. Type Objects and the type Type Object

It may seem unusual 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() BIF below, but for now, we want to let you know that you can find out the type of an object by calling type() with that object:

>>> type(42) <type 'int'>


Let us look at this example more carefully. It does not look tricky by any means, but examine the return value of the call. We get the seemingly innocent output of <type 'int'>, but what you need to realize is that this is not just a simple string telling you that 42 is an integer. What you see as <type 'int'> is actually a type object. It just so happens that the string representation chosen by its implementors has a string inside it to let you know that it is an int type object.

Now you may ask yourself, so then what is the type of any type object? Well, let us find out:

>>> type(type(42)) <type 'type'>


Yes, the type of all type objects is type. The type type object is also the mother of all types and is the default metaclass for all standard Python classes. It is perfectly fine if you do not understand this now. This will make sense as we learn more about classes and types.

With the unification of types and classes in Python 2.2, type objects are playing a more significant role in both object-oriented programming as well as day-to-day object usage. Classes are now types, and instances are now objects of their respective types.

4.3.2. None, Python's Null Object

Python has a special type known as the Null object or NoneType. It has only one value, None. The type of None is NoneType. It does not have any operators or BIFs. 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 (useful) attributes and always evaluates to having a Boolean False value.

Core Note: Boolean values

All standard type objects can be tested for truth value and compared to objects of the same type. Objects have inherent TRue or False values. Objects take a False value when they are empty, any numeric representation of zero, or the Null object None.

The following are defined as having false values in Python:

  • None

  • False (Boolean)

  • Any numeric zero:

  • 0 (integer)

  • 0.0 (float)

  • 0L (long integer)

  • 0.0+0.0j (complex)

  • "" (empty string)

  • [] (empty list)

  • () (empty tuple)

  • {} (empty dictionary)

Any value for an object other than those above is considered to have a true value, i.e., non-empty, non-zero, etc. User-created class instances have a false value when their nonzero (__nonzero__()) or length (__len__()) special methods, if defined, return a zero value.




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

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