< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204174062198212142209026187

Classes

Recall that a class is a data structure that we can use to define objects which hold together data values and behavioral characteristics. Classes are entities which are the programmatic form of an abstraction for a real-world problem, and instances are realizations of such objects. One analogy is to liken classes to blueprints or molds with which to make real objects (instances). So why the term "class?" The term most likely originates from using classes to identify and categorize biological families of species to which specific creatures belong and can be derived into similar yet distinct subclasses. Many of these features apply to the concept of classes in programming.

In Python, class declarations are very similar to function declarations, a header line with the appropriate keyword followed by a suite as its definition, as indicated below:

					
def functionName(args):
    'function documentation string'
    function_suite

class
						ClassName:
    'class documentation string"
    class_suite

				

The fact that such a declaration is "larger" than a standard type declaration should be proof that classes in Python are much more than standard types. A class is like a Python container type on steroids. Not only can it hold multiple data items but it can also support its own set of functions, which we have seen before, called methods. You may be asking what other advantages classes have over standard container types such as lists and dictionaries.

Standard types are fixed, cannot be customized, and come with a hard-coded set of attributes. Data types also do not provide individual namespaces for objects nor can they be used to derive "sub-types." Objects contained in lists are unrelated except for the name of their container. Its members are accessed only via an index offset into an array-like data structure. All lists have the same set of methods. The same goes for dictionaries, which also have a common set of methods and provide key access to their members (who are also unrelated except for their container name).

In this section, we will take a close look at classes and what types of attributes they have. Just remember to keep in mind that even though classes are objects (everything in Python is an object), they are not realizations of the objects they are defining. We will look at instances in the next chapter, so stay tuned for that. For now, the limelight is strictly beamed on class objects.

When you create a class, you are practically creating your own kind of data entity. All instances of that class are similar, but classes differ from each other (and so will instances of different classes by nature). Rather than playing with toys that came from the manufacturer and were bestowed upon you as gifts, why not design and build your own toys to play with?

Creating Classes

Python classes are created using the class keyword. In the simple form of class declarations, the name of the class immediately follows the keyword:

						
class ClassName:
     'class documentation string'
     class_suite

					

class_suite consists of all the component statements, defining class members, data attributes, and functions. Classes are generally defined at the top-level of a module so that instances of a class can be created anywhere in a piece of source code where the class is defined.

Declaration vs. Definition

As with Python functions, there is no distinction between declaring and defining classes because they occur simultaneously, i.e., the definition (the class suite) immediately follows the declaration (header line with the class keyword) and the always recommended, but optional, documentation string. Likewise, all methods must also be defined at this time. If you are familiar with the OOP terms, Python does not support pure virtual functions (à la C++) or abstract methods (as in Java), which coerce the programmer to define a method in a subclass.


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

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.