Cyber Monday Sale: save up to $108/year Cyber Monday Sale: subscribe by Nov 28 to save up to $108/year Learn more

What is a class?

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

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
1
>>> duck.price
5

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()
4

The profit_margin method accessed the name 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.

Summary

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".

Sale Through November 28 💸

Subscribers get customized weekly recommendations for exercises and screencasts and access over 300 screencasts, exercises, and articles.

Save up to $108 per year if you subscribe by November 28.

Save on Python screencasts and exercises

Series: Classes

Classes are a way to bundle functionality and state together. The terms "type" and "class" are interchangeable: list, dict, tuple, int, str, set, and bool are all classes.

You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
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.