XClose
Menu

Defining your own classes

User Defined Types

A class is a user-programmed Python type (since Python 2.2!)

It can be defined like:

In [1]:
class Room(object):
    pass

Or:

In [2]:
class Room():
    pass

Or:

In [3]:
class Room:
    pass

What's the difference? Before Python 2.2 a class was distinct from all other Python types, which caused some odd behaviour. To fix this, classes were redefined as user programmed types by extending object, e.g., class room(object).

So most Python 2 code will use this syntax as very few people want to use old style python classes. Python 3 has formalised this by removing old-style classes, so they can be defined without extending object, or indeed without braces.

Just as with other python types, you use the name of the type as a function to make a variable of that type:

In [4]:
zero = int()
type(zero)
Out[4]:
int
In [5]:
myroom = Room()
type(myroom)
Out[5]:
__main__.Room

In the jargon, we say that an object is an instance of a particular class.

__main__ is the name of the scope in which top-level code executes, where we've defined the class Room.

Once we have an object with a type of our own devising, we can add properties at will:

In [6]:
myroom.name = "Living"
In [7]:
myroom.name
Out[7]:
'Living'

The most common use of a class is to allow us to group data into an object in a way that is easier to read and understand than organising data into lists and dictionaries.

In [8]:
myroom.capacity = 3
myroom.occupants = ["James", "Sue"]

Methods

So far, our class doesn't do much!

We define functions inside the definition of a class, in order to give them capabilities, just like the methods on built-in types.

In [9]:
class Room(object):
    def overfull(self):
        return len(self.occupants) > self.capacity
In [10]:
myroom = Room()
myroom.capacity = 3
myroom.occupants = ["James", "Sue"]
In [11]:
myroom.overfull()
Out[11]:
False
In [12]:
myroom.occupants.append(['Clare'])
In [13]:
myroom.occupants.append(['Bob'])
In [14]:
myroom.overfull()
Out[14]:
True

When we write methods, we always write the first function argument as self, to refer to the object instance itself, the argument that goes "before the dot".

This is just a convention for this variable name, not a keyword. You could call it something else if you wanted.

Constructors

Normally, though, we don't want to add data to the class attributes on the fly like that. Instead, we define a constructor that converts input data into an object.

In [15]:
class Room(object):
    def __init__(self, name, exits, capacity, occupants=[]):
        self.name = name
        self.occupants = occupants  # Note the default argument, occupants start empty
        self.exits = exits
        self.capacity = capacity

    def overfull(self):
        return len(self.occupants) > self.capacity
In [16]:
living = Room("Living Room", {'north': 'garden'}, 3)
In [17]:
living.capacity
Out[17]:
3

Methods which begin and end with two underscores in their names fulfil special capabilities in Python, such as constructors.

Object-oriented design

In building a computer system to model a problem, therefore, we often want to make:

  • classes for each kind of thing in our system
  • methods for each capability of that kind
  • properties (defined in a constructor) for each piece of information describing that kind

For example, the below program might describe our "Maze of Rooms" system:

We define a "Maze" class which can hold rooms:

In [18]:
class Maze(object):
    def __init__(self, name):
        self.name = name
        self.rooms = {}

    def add_room(self, room):
        room.maze = self  # The Room needs to know 
                          # which Maze it is a part of
        self.rooms[room.name] = room

    def occupants(self):
        return [occupant for room in self.rooms.values()
                for occupant in room.occupants.values()]

    def wander(self):
        """Move all the people in a random direction"""
        for occupant in self.occupants():
            occupant.wander()

    def describe(self):
        for room in self.rooms.values():
            room.describe()

    def step(self):
        self.describe()
        print("")
        self.wander()
        print("")

    def simulate(self, steps):
        for _ in range(steps):
            self.step()

And a "Room" class with exits, and people:

In [19]:
class Room(object):
    def __init__(self, name, exits, capacity, maze=None):
        self.maze = maze
        self.name = name
        self.occupants = {}  # Note the default argument, occupants start empty
        self.exits = exits  # Should be a dictionary from directions to room names
        self.capacity = capacity

    def has_space(self):
        return len(self.occupants) < self.capacity

    def available_exits(self):
        return [exit for exit, target in self.exits.items()
                if self.maze.rooms[target].has_space()]

    def random_valid_exit(self):
        import random
        if not self.available_exits():
            return None
        return random.choice(self.available_exits())

    def destination(self, exit):
        return self.maze.rooms[self.exits[exit]]

    def add_occupant(self, occupant):
        occupant.room = self  # The person needs to know which room it is in
        self.occupants[occupant.name] = occupant

    def delete_occupant(self, occupant):
        del self.occupants[occupant.name]

    def describe(self):
        if self.occupants:
            print(f"{self.name}: " + " ".join(self.occupants.keys()))

We define a "Person" class for room occupants:

In [20]:
class Person(object):
    def __init__(self, name, room=None):
        self.name = name

    def use(self, exit):
        self.room.delete_occupant(self)
        destination = self.room.destination(exit)
        destination.add_occupant(self)
        print("{some} goes {action} to the {where}".format(some=self.name,
                                                           action=exit,
                                                           where=destination.name))

    def wander(self):
        exit = self.room.random_valid_exit()
        if exit:
            self.use(exit)

And we use these classes to define our people, rooms, and their relationships:

In [21]:
james = Person('James')
sue = Person('Sue')
bob = Person('Bob')
clare = Person('Clare')
In [22]:
living = Room('livingroom', {'outside': 'garden',
                             'upstairs': 'bedroom', 'north': 'kitchen'}, 2)
kitchen = Room('kitchen', {'south': 'livingroom'}, 1)
garden = Room('garden', {'inside': 'livingroom'}, 3)
bedroom = Room('bedroom', {'jump': 'garden', 'downstairs': 'livingroom'}, 1)
In [23]:
house = Maze('My House')
In [24]:
for room in [living, kitchen, garden, bedroom]:
    house.add_room(room)
In [25]:
living.add_occupant(james)
In [26]:
garden.add_occupant(sue)
garden.add_occupant(clare)
In [27]:
bedroom.add_occupant(bob)

And we can run a "simulation" of our model:

In [28]:
house.simulate(3)
livingroom: James
garden: Sue Clare
bedroom: Bob

James goes north to the kitchen
Sue goes inside to the livingroom
Clare goes inside to the livingroom
Bob goes jump to the garden

livingroom: Sue Clare
kitchen: James
garden: Bob

Sue goes outside to the garden
Clare goes outside to the garden
James goes south to the livingroom
Bob goes inside to the livingroom

livingroom: James Bob
garden: Sue Clare

James goes north to the kitchen
Bob goes upstairs to the bedroom
Sue goes inside to the livingroom
Clare goes inside to the livingroom

Object oriented design

There are many choices for how to design programs to do this. Another choice would be to separately define exits as a different class from rooms. This way, we can use arrays instead of dictionaries, but we have to first define all our rooms, then define all our exits.

In [29]:
class Maze():
    def __init__(self, name):
        self.name = name
        self.rooms = []
        self.occupants = []        
        
    def add_room(self, name, capacity):
        result = Room(name, capacity)
        self.rooms.append(result)
        return result

    def add_exit(self, name, source, target, reverse=None):
        source.add_exit(name, target)
        if reverse:
            target.add_exit(reverse, source)

    def add_occupant(self, name, room):
        self.occupants.append(Person(name, room))
        room.occupancy += 1

    def wander(self):
        "Move all the people in a random direction"
        for occupant in self.occupants:
            occupant.wander()

    def describe(self):
        for occupant in self.occupants:
            occupant.describe()

    def step(self):
        self.describe()
        print("")
        self.wander()
        print("")

    def simulate(self, steps):
        for _ in range(steps):
            self.step()
In [30]:
class Room:
    def __init__(self, name, capacity):
        self.name = name
        self.capacity = capacity
        self.occupancy = 0
        self.exits = []

    def has_space(self):
        return self.occupancy < self.capacity

    def available_exits(self):
        return [exit for exit in self.exits if exit.valid()]

    def random_valid_exit(self):
        import random
        if not self.available_exits():
            return None
        return random.choice(self.available_exits())

    def add_exit(self, name, target):
        self.exits.append(Exit(name, target))
In [31]:
class Person():
    def __init__(self, name, room=None):
        self.name = name
        self.room = room

    def use(self, exit):
        self.room.occupancy -= 1
        destination = exit.target
        destination.occupancy += 1
        self.room = destination
        print("{some} goes {action} to the {where}".format(some=self.name,
                                                           action=exit.name,
                                                           where=destination.name))

    def wander(self):
        exit = self.room.random_valid_exit()
        if exit:
            self.use(exit)

    def describe(self):
        print("{who} is in the {where}".format(who=self.name,
                                               where=self.room.name))
In [32]:
class Exit:
    def __init__(self, name, target):
        self.name = name
        self.target = target

    def valid(self):
        return self.target.has_space()
In [33]:
house = Maze('My New House')
In [34]:
living = house.add_room('livingroom', 2)
bed = house.add_room('bedroom', 1)
garden = house.add_room('garden', 3)
kitchen = house.add_room('kitchen', 1)
In [35]:
house.add_exit('north', living, kitchen, 'south')
In [36]:
house.add_exit('upstairs', living, bed, 'downstairs')
In [37]:
house.add_exit('outside', living, garden, 'inside')
In [38]:
house.add_exit('jump', bed, garden)
In [39]:
house.add_occupant('James', living)
house.add_occupant('Sue', garden)
house.add_occupant('Bob', bed)
house.add_occupant('Clare', garden)
In [40]:
house.simulate(3)
James is in the livingroom
Sue is in the garden
Bob is in the bedroom
Clare is in the garden

James goes north to the kitchen
Sue goes inside to the livingroom
Bob goes jump to the garden
Clare goes inside to the livingroom

James is in the kitchen
Sue is in the livingroom
Bob is in the garden
Clare is in the livingroom

Sue goes outside to the garden
Bob goes inside to the livingroom
Clare goes upstairs to the bedroom

James is in the kitchen
Sue is in the garden
Bob is in the livingroom
Clare is in the bedroom

James goes south to the livingroom
Bob goes north to the kitchen
Clare goes jump to the garden

This is a huge topic, about which many books have been written. The differences between these two designs are important, and will have long-term consequences for the project. That is the how we start to think about software engineering, as opposed to learning to program, and is an important part of this course.

Exercise: Your own solution

Compare the two solutions above. Discuss with a partner which you like better, and why. Then, starting from scratch, design your own. What choices did you make that are different from mine?