Python Objects and Classes

Objects are like variables, but they have the ability to be more complicated and powerful.

A class defines a type of object.

The class is the blueprint for creating a type of object. The class defines the attributes (object variables) and methods (object functions) that belong to each object of that class.

One a class is defined, it can be used to create many objects of that type. Each object can store its own unique attribute values. Each object has a distinct identity in your computer’s memory, even if its attribute values happen to be the same as another object’s.


Example: 

Let’s say we have a game where you train workers to accumulate resources. Your workers produce more if you train them to increase their level.

class Worker:
    def __init__(self):
        self.level = 1

    def work(self):
        print('Produced', self.level)
        return self.level

    def train(self):
        self.level += 1
        print('Trained to level', self.level)

resources = 0
w1 = Worker()
resources += w1.work()
w1.train()
resources += w1.work()
print('Resources:', resources)

First the class is defined, starting with the first line of code.

This class has three methods, which are functions that objects of this class are able to call. Methods are behaviors of this type of object.

  • The name of the class is Worker.
  • After the colon in “class Worker:” everything that’s indented is part of this class definition.
  • A Worker object named w1 is created with “w1 = Worker()”
  • Worker() causes the __init__(self) function to be called, which creates a Worker object and in this case assigns it to the variable w1.
  • Any time you call a function with the same name as a class, it calls that class’ __init__(self) function.
  • __init__(self) is a constructor function, meaning it creates an object of its class. Init stands for “initialize” since it causes the object’s variables to be initialized.

Creating Objects

This line creates a Worker and assigns it to the variable w1:

w1 = Worker()

Worker() is a function call for the constructor function belonging to the Worker class. (Notice the (parentheses) after the function call)

When a constructor is called, the __init__(self) function of the class is called. It sets its level to 1 with this line of code:

self.level = 1

“Init” stands for initialize since these methods typically initialize the object’s attributes (variables).

Accessing Object Attributes

print(w1.level)
w1.level = 3
w1.level += 2

To access an object’s attributes, follow this pattern:

object_name.attribute_name

(object’s name first, followed by a dot, then name of the attribute)

Calling Object Methods (Functions):

w1.work()
w1.train()

object_name.method_name()

(object’s name first, followed by a dot, then method name with parentheses)

References to self

When an object’s function needs to refer to itself, we use the word self to represent the object name.

You can’t use a specific variable name like w1 in a function definition belonging to a class. Those functions need to be generic and work for any worker object, not just an object named w1.

The self argument appears in the function declaration for a class function, as seen in train(self):

def train(self):
    self.level += 1
    print('Trained to level', self.level)

Having self in the argument list (must always be the first argument) means that this method has access to the object itself. It can read or modify itself.

The self argument does not need to be included when the function is called. That happens automatically. That’s why the later code says w1.train() instead of w1.train(self).

The line “self.level +=1” means “increase my own level by 1.”

When do I use self and when do I use the variable name?

Answer: use self inside the class declaration, and use the variable name outside of the class.