The Tutorial:
 
 


    Lesson 6: Classes in Python

    Classes are the way to develop object-oriented programming (OOP) in Python.  Classes allow programmers to generate new objects in the Python language, as well as specialize, or inherit from, more general objects.  In Python, the reserved word class allows programmers to create a new object type with special values and properties. Each class declaration creates a new instance with its own namespace [4, 2: 151 - 153].

    To help clear up a very foggy aspect of OOP, let's look at an example.

    simple_data_class.py: 

    # Define the class object
    class simple_data:
    # Define member functions
    def set_name(self, info):
            self.name = info
    def display_name(self):
            print self.name

    In Python, the reserved word self is similar to this in C++. The word self refers to the instance under operation.  Rather than passing the object as a parameter, the word self refers to the object on which the '.' qualifier operates (in Code Example 6.1, store and item).  Code Example 6.1 demonstrates the use of the simple_data class.  This example also uses the reserved word from, which when coupled with the import function allows a client to select a specific class from a module.

    Code Example 6.1: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Import the outside class.
    from simple_data_class import simple_data

    # Use default constructors to create 'simple_data' objects.
    store = simple_data()
    item = simple_data()

    # Assign them values.
    store.set_name("Super Store")
    item.set_name("Month supply of tissue")

    # Print the values.
    store.display_name()
    item.display_name()

    Suppose we wanted to make a special version of simple_data that has a different version of display_name.  This can be accomplished through inheritence.  As shown in still_simple_data.py, the class definition syntax is much the same, except we note the class we wish to inhert from within parenthesis.  still_simple_data objects retain the set_name member function from the simple_data class, but now they have their own version of print [2: 154 - 155].

    still_simple_data_class.py: 

    # Define the class object
    class simple_data:
    # Define member functions
    def set_name(self, info):
            self.name = info
    def display_name(self):
            print self.name

    # Define a subclass object
    class still_simple_data(simple_data):
    # Define its own member function version of display_name
    def display_name(self):
            print "The name is:", self.name

    Code Example 6.2 shows both classes in action.

    Code Example 6.2: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Import all(*) exports from the outside class.
    from still_simple_data_class import *

    # Use default constructors to create objects.
    store = simple_data()
    item = still_simple_data()

    # Assign them values.
    store.set_name("Super Store")
    item.set_name("Month supply of tissue") # Use superclass definition

    # Print the values.
    store.display_name()
    item.display_name() # Use subclass definition

    Although the module and class examples in this tutorial are quite trivial, the power these features add to the language is very important.  Modules and classes make Python extremely extensible.  As long as the external interface to a class is kept constant, the underlying implementation can be reworked for future needs.  This property of the language is invaluable in large-scale projects.

    To Lesson 7 ...

 © 2000 Department of Computer Science, University of Virginia