Learn Django

Understanding Classes

We take a brief break from Django to discuss a key Python concept; classes, inheritance and mixins. These are important concepts to understand because our project uses this functionality of Python several times. In some cases, Django forces the use of these features, therefore it's important you understand them. If you already know what these are you can skip to the next lesson, which not so coincidentally uses class inheritance.

A Brief Intro to Python Classes

Classes are used to store information in a record-like format to avoid code redundancy and allow us to create methods that can use that data in a single place. Classes also allow us to expand on and store data in a way that other Python structures like lists and dictionaries do not. There are several reasons why we would want to use classes to create objects in Python:

  • Encapsulation - the idea that we can wrap up information about an object in a logical way with easy accessibility. This also creates a way that we can create method functionality that deals directly and specifically with this data.
  • Code Structure - Classes provide a separate program unit that combines the logic and data for a specific object in a single package.
  • Growth - Object-oriented programming allows classes to be extended as the functionality in your program grows and changes. Customization is allowed by extending classes or creating new subclasses without having to break or redo existing code.

For large systems that change over time, the benefits of object-oriented programming are obvious. Learning object-oriented programming in Python is easy so let’s look at an example class.

1
2
3
class person:
    firstname = "Mark"
    lastname = "Smith"

To create a class we use the class keyword, followed by the name of the class. We can then add named members to the class with values. Here is how we access the class in our program.

1
print person.firstname

So we would get “Mark” when we printed. To create more than one instance of an object you would equal that to a variable to hold it.

1
2
person1 = person()
person2 = person()

Now we have two people but both of them have that same first and last name. We can change this information for one of them like this.

1
2
person2.firstname = "Steve"
person2.lastname = "Johnson"

We have now changed person2 to have the name Steve Johnson and person1 would still have the name Mark Smith. These two versions of the person class are considered separate instances and can be changed independently of each other. These person object variables can then be stored in a List or Dictionary so you can move through the data as needed.

What is Class Inheritance?

Class inheritance is basing one class from another. Let's say you have three classes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Inventory(object):
    quantity = 0
    shelf = None
    product_code = None

class Cereal(Inventory):
    name = "Cocoa Puffs"
    box_color = "brown"

class Soup(Inventory):
    name = "Campbell"
    flavor = "Chili"

In the above example we created a class named Inventory that defines a quantity, shelf, and product_code attribute. We then created two additional classes that inherited the Inventory class. What this means is that every Cereal an Soup instance will have the quantity, shelf, and product_code attributes, in addition to the attributes that were defined within their primary class. That's class inheritance.

What are Class Mixins?

Class mixins is almost exactly like inheritance with one subtle difference. Class mixins are classes that are not designed to be used on their own. Rather, they're a class that likely contains features that are meant to be inherited to many other classes. In our example above, the Inventory class could actually be considered a mixin if it was never designed to be used on its own (meaning no instances are ever created directly from it; it's only use is to be inherited by other classes).

Track your progress with a free account