Classes and Objects

Tutorial

Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

A very basic class would look something like this:

 class MyClass:
      variable = "blah"

      def function(self):
           print "This is a message inside the class."

We'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following:

 myobjectx = MyClass()

Now the variable "myobjectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".

Accessing Object Variables

To access the variable inside of the newly created object "MyObject" you would do the following:

 myobjectx.variable

So for instance the below would output the string "blah":

 print myobjectx.variable

You can create multiple different objects that are of the same class(have the same variables and functions defined). However, each object contains independent copies of the variables defined in the class. For instance, if we were to define another object with the "MyClass" class and then change the string in the variable above:

 myobjecty = MyClass()
 myobjecty.variable = "yackity"

Then print out both values:

 print myobjectx.variable   # This would print "blah".
 print myobjecty.variable   # This would print "yackity".

Accessing Object Functions

To access a function inside of an object you use notation similar to accessing a variable:

 myobjectx.function()

The above would print out the message, "This is a message inside the class."

Exercise

We have a class defined for vehicles. Create two new vehicles called MyCar1 and MyCar2. Set MyCar1 to be a red convertible worth $60,000 with a name of Fer, and MyCar2 to be a blue van named Jump worth $10,000.

Tutorial Code

#define the Vehicle class
class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00 
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str
#your code goes here

#checking code
print MyCar1.description()
print MyCar2.description()

Expected Output

Fer is a red convertible worth $60000.00.
Jump is a blue van worth $10000.00.