Classes are a way to bundle functionality and state together.
The terms "type" and "class" are interchangeable:
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.
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.
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
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.
The object-oriented Python world has a lot of redundant and overlapping terminology.
Here are three different ways to say the same thing:
Productclass, you are instantiating a new
Productclass, you are constructing a new
Productclass, you are making a
Product instance, a
Product object, and just a
Product, all mean the same thing; that is an object whose type (whose class) is
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:
We have a
Product object here, that we are pointing to with
duck variable (see figure below). So, the
duck variable points to a
Product instance, or a
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
>>> 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
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.
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
profit_margin method accessed the
price attributes on our
Product instance and subtracted them to get
Methods tend to either access data from a class instance (as we're doing here) or change the data in a class instance.
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 wanna 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
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.