Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
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.
classdefinition in Python Terminology.
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.
The object-oriented Python world has a lot of redundant and overlapping terminology.
Here are three different ways to say the same thing:
Product
class, you are instantiating a new Product
instance.Product
class, you are constructing a new Product
object.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
.
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 the duck
variable (see figure below). So, the duck
variable points to a Product
instance, or a Product
object.
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.
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.
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
".
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll explain concepts that new Python programmers often overlook.
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.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.