Python Concepts/Classes

Lesson
Python classes range from the simple to the complicated. We're already familiar with several of python's simple classes:  Classes more sophisticated include and

Python's builtin function  shows the class of an object. Many classes become available after the appropriate module has been imported. The real power of python lies in the fact that each user of python may create new classes as simply as writing new functions. And each new class easily encapsulates features that enhance the class.

Object Oriented Programming
OOP is a programming approach where objects are defined with methods (functions, actions or events) and properties (values, characteristics), resulting in more readable, more reusable code.

Lets say you're writing a program where you need to keep track of multiple cars. Each car has different characteristics like mileage, color, and top speed, but lucky for us they all can perform some common actions like braking, accelerating, and turning.

Instead of writing code separately for each car we could create a class called 'Car' that will be the blueprint for each particular car.

Constructing a class
Class is the name given to a generic description of an object. In python you define a class method (an action, event, or function) using the following structure:

Let's take a detailed look. We define our object using the 'class' keyword, the name we want, and a colon. We define its methods as we would a normal function: only one indent with 'self' as its first argument (we get to this later). So our example car class may look like this:

But how do I use it?
Once you have created the class, you actually need to create an object for each instance of that class. In python we create a new variable to create an instance of a class. Example: Using the parentheses ("calling" the class) tells Python that you want to create an instance and not just copy the class definition. You would need to create a variable for each car. However, now each car object can take advantage of the class methods and attributes, so you don't need to write a brake and accelerate function for each car independently.

Properties
Right now all the cars look the same, but let's give them some properties to differentiate them. A property is just a variable that is specific to a given object. To assign a property we write it like: And retrieve its value like:

It is good programming practice to write functions to get (or retrieve) and set (or assign) properties that are not 'read-only'. For example: Notice the single underscore before the property name; this is a way of hiding variable names from users.

Beginning from Python 2.2, you may also define the above example in a way that looks like a normal variable: Then, when you do like mycar.owner = "John Smith", the set_owner function is instead called transparently.

Extending a class
Let's say we want to add more functions to our class, but we are reluctant to change its code for fear that this might mess up programs that depend on its current version. The solution is to 'extend' our class. When you extend a class you inherit all the parent methods and properties and can add new ones. For example, we can add a start_car method to our car class. To extend a class we supply the name of the parent class in parentheses after the new class name, for example: This new class extends the parent class.

Special Class Methods
In Python the names of special methods begin and end with double underscore - __. For example, the special method __init__ is used to initialize the state of newly created objects.For instance, we could create a new car object and set its brand, model, and year attributes on a single line, rather than expending an additional line for each attribute:

For more information on classes go to the Class Section in Wikibooks.

class Queue
The following code implements  It serves as an example of a not-too-complicated class with attributes (usually not visible to the user) and methods that are used to implement FIFO (first in, first out) functionality.

Examples
Create a new Queue and print contents:

Print the __doc__ string:

Make a shallow or soft copy: Make a deep or hard copy:

Print long strings:

Get data from queue:

Examples of errors:

Further Reading or Review

 * Previous Lesson: Modules
 * This Lesson: Classes
 * Next Lesson: Regular Expressions
 * Course Home Page