Classes
Remember working with turtle? Remember how you had to use .function() instead of just writing function()? That's because a turtle is actually a class object, which is what you'll learn about here.
Remember working with turtle? Remember how you had to use .function() instead of just writing function()? That's because a turtle is actually a class object, which is what you'll learn about here.
In Python, a class is simply an object. It's a special type of object, one that can have variables unique to it, functions unique to it, and it can be a "subset" of something else. For example, look at the diagram below:
It shows different types of things and how they are connected. For example, the diagram shows that "Giraffes" is included in "Animals", which is included in "Things".
A class is basically a blueprint for how to make objects. For example, there is a list class, which we learnt about already. It has predefined methods for lists in general. We can then make an individual list, or an instance of a list.
The basic format of a class is:
class name[(inheritFrom)]:
variables are defined here...
so are subclasses...
so are functions...
anything else produces an error...
The inheritFrom
parameter for a class lets us create a class that has all the variables, methods, and subclasses of an old class.
Let's turn that diagram above into a set of classes! We'll start with the Things
class:
class Things:
pass
Remember the pass statement from earlier? Since there's nothing in our class yet, we'll use pass
to prevent Python from giving us errors. Next, we'll make the Animals
and Food
classes:
class Animals(Things):
pass
class Food(Things):
pass
Did you see that we now have Things
in parentheses? This lets Python know that we want Animals
and Food
to inherit all of the things we defined in Things
. This means that objects created with Animals
and Food
can also use methods defined in Things
. The last two classes we'll make are Giraffes
and Penguins
. Here's all our code:
class Things:
pass
class Animals(Things):
pass
class Food(Things):
pass
class Giraffes(Animals):
pass
class Penguins(Animals):
pass
Now, let's try adding some properties and methods!
To add a property, which is basically a variable, we act as if we're making a variable. Let's give each animal a name:
class Animals(Things):
name = ""
To add a method, which is basically a function, we act as if we're making a function. However, there is one thing we need to know, which is that functions made in classes always have at least one required parameter, called self
. Here's an example class:
class Example:
def func(self):
pass
The self
parameter can be named anything you like as long as it's the first parameter. Without that self
parameter, Python will produce an error.
Let's add methods that make animals move and eat food:
class Animals(Things):
name = ""
def eat(self):
print("Eating food...")
def move(self):
print("Moving around...")
We've defined a lot of methods and properties here. Let's try them out! To start, we're going to make a new Animals
object, or instance.
An object/instance is basically a variable that has properties and variables defined by a class. For example, remember working with turtle
?
from turtle import *
t = Pen()
That variable t
is an instance of the class Pen
. Once we've created t
, we can call methods on it, such as .forward(50)
.
Let's make an Animals
instance! We're going to store the instance in a variable called alex
:
alex = Animals()
Make sure to remember the parentheses! Without the parentheses, you're creating a new class called alex
, not an instance.
Now that we've created alex
, we can call methods on it, and get/set property values. Let's make alex
eat food:
alex.eat() # This should print "Eating food..."
We can also get alex
's name:
print(alex.name) #
Did you notice that there were no parentheses after .name
? That's because .name
is a property, not a function. Currently, alex
's name is "". However, we can change it as if we were setting a variable:
alex.name = "Alex"
print(alex.name) # Alex
Python, however, has a way to change that property when we make a new instance. This is with magic methods.
Python has something called magic methods. Magic methods can change what happens when two variables are added, when an instance is initiated, etc. The most useful magic method for classes is the __init__
method, which we're going to learn about now.]
Defining magic methods is as easy as defining regular functions in a class. For example, to defined the __init__
magic method, we write:
class classname:
def __init__(self,params):
code...
code...
The __init__
method is actually run when you create an instance:
alex = Animals()
Currently, our __init__
function does nothing. However, we can override it to change the name when a new Animals
instance is created. Let's try that:
class Animals(Things):
name = ""
def eat(self):
print("Eating food...")
def move(self):
print("Moving around...")
def __init__(self,name):
self.name = name
Let's take a closer look at our __init__
function so I can explain a few things:
def __init__(self,name):
self.name = name
The first line, def __init__(self,name):
defines the __init__ magic method. Like every other class function, its first parameter should be self
, so we can access the object being created. The name
parameter tells Python that when an object is initiated, it should have a name parameter.
The second line, self.name = name
, tells Python to store the name provided into the object's name property.
Now, let's try redefining alex
:
alex = Animals("Alex")
Now, let's try getting the name property of alex
:
print(alex.name) # Alex
It worked!