4.3. Other Built-in Types
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 ObjectIt 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 ObjectPython 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
|