What is a class?

Copied to clipboard.
Series: Classes
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 5 min. video Python 3.8—3.12

Classes are for coupling state and functionality. You've got some data and some actions you'd like to perform on that data. You want to bundle those two concepts together. That's what a class is for in Python.

Defining a class

We define a class by using the class keyword. That's how we start a class definition:

class Product:

    def __init__(self, name, cost, price):
        self.name = name
        self.cost = cost
        self.price = price

    def profit_margin(self):
        return self.price - self.cost

This is similar to how you define a function by using the def keyword.

Once we've defined a class, we can call it. Calling a class is a little bit different than calling a function. When you call a function, you get the return value of the function. When you call a class, you get an object whose type is that class:

>>> from product import Product
>>> duck = Product(name="rubber duck", cost=1, price=5)
>>> duck
<product.Product object at 0x7f584c643310>
>>> type(duck)
<class 'product.Product'>

The words type and the word class are basically interchangeable in Python. The type of something is its class.

Strange object-oriented terminology

The object-oriented Python world has a lot of redundant and overlapping terminology.

Here are three different ways to say the same thing:

  1. When you call the Product class, you are instantiating a new Product instance.
  2. Also, when you call the Product class, you are constructing a new Product object.
  3. And, when you call the Product class, you are making a Product.

So, a Product instance, a Product object, and just a Product, all mean the same thing; that is an object whose type (whose class) is Product.

Working with Python objects

Once you've made an instance of a class (an object whose type is that class) there are two main things that you can do with that object:

  1. You can get the data that's stored within that instance
  2. You can perform actions on that instance

We have a Product object here, that we are pointing to with the duck variable (see figure below). So, the duck variable points to a Product instance, or a Product object.

Attributes: variables that live on class instances

We can access the data that's stored on this Product instance by looking up its attributes.

You can access an attribute by taking a reference to the Product object, putting a . after it, and then putting the name of the attribute. Here are the name, cost, and price attributes:

>>> duck.name
'rubber duck'
>>> duck.cost
>>> duck.price

You can think of an attribute as kind of like a variable name that lives specifically on one object, specifically on a Product instance in this case.

These attributes would be different for different Product instances. So, if we had a Product object named "stuffed unicorn", it would have different attributes on it.

So that's how we access the data that's on a class instance.

Methods: functions that live on classes

What about performing actions on a class?

You can perform actions on a class by using methods. A method is basically a function that lives on a class and specifically operates on instances of that class.

To use our profit_margin method, we can look up the profit_margin attribute on a Product instance and put parentheses after it to call it:

>>> duck.profit_margin()

The profit_margin method accessed the cost and price attributes on our Product instance and subtracted them to get 4.

Methods tend to either access data from a class instance (as we're doing here) or change the data in a class instance.

Python's classes couple data and functionality

So, classes in Python take data and functionality and couple them together.

When you call a class, the thing you get back is an instance of that class. Once you've got that instance, you can get the data through attributes. If you'd like to perform actions on that class instance, you can do that by calling methods on that class instance.

Also the phrase "instance of class Product" means the same thing as "an object whose type is Product".

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.