Classes are everywhere in Python. Even if you never define your own type of class, you will certainly use classes in Python.
>>> from point import Point >>> p = Point(1, 2, 3)
We can access the
x attribute? on this
Point object (aka
Point instance) or call the
is_origin method on it:
>>> p.x 1 >>> p.is_origin() False
When we check the
p (which is a
Point instance) it will tell us I am an object of type "class
>>> type(p) <class 'point.Point'>
The type of something is the same as the class of that thing.
Class instances (aka objects of some class/type) can have attributes and they can have methods.
We've seen attributes and methods in other places in Python too.
For example, lists have an
>>> numbers = [1, 2, 3] >>> numbers.append(4) >>> numbers [1, 2, 3, 4]
If you ask lists for their
type, you'll see some kind of class. You always get a class when you ask for the type of something in Python:
>>> type(numbers) <class 'list'>
The type of a list is the
list is not a function in Python, it's a class.
When you call
list with parentheses after it, you get an empty list back because we're constructing a new object) of type
>>> list() 
All the built-in data structures in Python are classes:
>>> tuple <class 'tuple'> >>> dict <class 'dict'> >>> set <class 'set'>
Interestingly, some of the even more fundamental types of objects are actually classes.
Anything that has a
type has a class.
When you ask for the type of a string you'll see
>>> name = "Trey" >>> type(name) <class 'str'>
We may have thought of
str as a function that can convert something to a string:
>>> str(4) '4'
But it's actually a class!
int is also a class:
>>> x = 4 >>> type(x) <class 'int'>
in fact, there are even attributes we can access on instances of the
>>> x.real 4 >>> x 4
We're accessing the
real attribute of object to get the real component of this number, which is the same as the number itself.
So, Integers are a class, which means if we call the
int function, we're actually calling a class to get back an integer instance:
>>> int('4') 4
A lot of the built-in functions in Python are actually classes too.
For example, when we call the
enumerate function, we'll get back an
>>> enumerate(numbers) <enumerate object at 0x7f58e8228c00>
enumerate "function" is actually a class:
>>> enumerate <class 'enumerate'>
It's not incorrect to say "the
enumerate function" even though
enumerate is implemented as a class because the distinction between classes and functions isn't very important in Python.
The distinction between classes and functions doesn't really matter that much in Python. The thing that I care about is the behavior: I care that I can call it.
When you call a function, we'll get the return value of that function. When you call a class, we'll get an instance of that class. Both functions and classes are callables and that's the thing we usually care about ("can I call it?").
So classes are everywhere in Python! Even if you never make your own class, you will certainly use classes.
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.
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.
Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.