-
Step 1
Find the classes. If your program is already written as a collection of functions, you will notice some functions group well together--they all do related operations on related data. These functions and the related data are good candidates for abstraction with a class.
-
Step 2
Write down a list of all the "things" in your program if your Python program hasn't yet been written. The "things" are the problem-space object with which you're dealing. Good examples include: images to be displayed on screen, things to be controlled in the physical world, network connections to other computers or programs and internal resources used by your program.
-
Step 3
Organize your classes. Once you have a list of related functions or "things" in your program, collect them into related groups of classes. These related classes can often be written in a separate file called a module. Maintaining an independent module is a good way to make large programs manageable.
-
Step 4
Name your classes, giving them short, descriptive monikers that tell what the class is. Be sure you'll recognize the class name easily and it's not too difficult to type. If you're going to be using the class often, you might be typing the class name often. Examples of good class names are Image or Connection.
-
Step 1
Write empty class statements for all your classes. Organize them in a pleasing way now, since it's more difficult to move them after they're full of methods and data. These class skeletons should express the name of the class, as well as any relationships to other classes (inheritance, dependence). Add any comments about the classes' use or internal function that you wish to note.
-
Step 2
Write your class skeletons. In Python, a block cannot be empty, it must have at least one statement. In this example, the "pass" statements are placeholders--they're "do nothing" statement that can be used as a placeholder.
# An image to be displayed on the screen
class Image:
pass
# Remotely controlled microwave oven
class Microwave:
pass
# Connection to sever controlling microwave oven
class Connection:
pass -
Step 3
Fill the classes with functions and data. If you already have the code in functions outside the class, it will require some minor modifications. If you already have the data in parameters or global variables, move them into the class. A class should be a complete package, containing all data and functions required to act on that data. For example:
class Image:
filename = ""
image_handle = 0
def __init__(s,filename):
s.filename = filename
image_handle = load_image(filename)
def display(s,window):
window.display(image_handle) -
Step 4
Test the classes thoroughly. Test every method, write a test program with a known outcome and compare the output from your class. Identifying problems early and fixing them can save major debugging headaches later on.






