eHow launches Android app: Get the best of eHow on the go.

How To

How to Use Classes in Python

Member
By Amanda Morin
User-Submitted Article
(4 Ratings)

Python is an object-oriented language. Simply put, this means you can abstract any task as a class. This helps organize, modularize and transport your code. Taking advantage of polymorphism and inheritance, can also make your code more flexible and easier to change.

Difficulty: Moderate
Instructions

    Name and Organize Classes

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Use Classes

  6. 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.

  7. 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

  8. 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)

  9. 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.

Tips & Warnings
  • Don't make the mistake of naming a class by what it does or how it works. Examples of ineffective class names are RasterImage (how the image is displayed doesn't matter to the high-level programmer) or TCPConnectionToRemoteProgram (too descriptive, too long).
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics