If you would like a copy of the code used in this post, you can download it here.
This post aims to give a short, basic introduction to the concept of classes and inheritance, using Python as the language of choice. It assumes knowledge of very basic Python syntax and functions. These examples were conceived during the Boston Python Workshop this past weekend.
What is a Class?
Data structures like lists and strings are extremely useful, but sometimes they aren’t enough to represent something you’re trying to implement. For example, let’s say we needed to keep track of a bunch of pets. We could represent a pet using a list by specifying the first element of the list as the pet’s name and the second element of the list as the pet’s species. This is very arbitrary and nonintuitive, however – how do you know which element is supposed to be which?
Classes give us the ability to create more complicated data structures that contain arbitrary content. We can create a class that keeps track of the name and species of the pet in usefully named attributes called and , respectively.
What is an Instance?
Before we get into creating a class itself, we need to understand an important distinction. A class is something that just contains structure – it defines how something should be laid out or structured, but doesn’t actually fill in the content. For example, a class may say that a pet needs to have a name and a species, but it will not actually say what the pet’s name or species is.
This is where instances come in. An instance is a specific copy of the class that does contain all of the content. For example, if I create a pet , with name and species , then is an instance of .
This can sometimes be a very difficult concept to master, so let’s look at it from another angle. Let’s say that the government has a particular tax form that it requires everybody to fill out. Everybody has to fill out the same type of form, but the content that people put into the form differs from person to person. A class is like the form: it specifies what content should exist. Your copy of the form with your specific information if like an instance of the class: it specifies what the content actually is.
Keeping Track of Pets
Now that we have an idea of what a class is and what the difference between a class and an instance is, let’s look at a real class!
This is the basic incant for creating a class. The first word, , indicates that we are creating a class. The second word, , indicates the name of the class. The word in parentheses, , is the class that is inheriting from. We’ll get more into inheritance below, so for now all you need to know is that is a special variable in Python that you should include in the parentheses when you are creating a new class.
When we create a new pet, we need to initialize (that is, specify) it with a and a . The method (method is just a special term for functions that are part of a class) is a special Python function that is called when an instance of a class is first created. For example, when running the code , the method is called with values , , and for the variables , , and , respectively.
The variable is the instance of the class. Remember that instances have the structure of the class but that the values within an instance may vary from instance to instance. So, we want to specify that our instance () has different values in it than some other possible instance. That is why we say instead of .
You might have noticed that the method (as well as other methods in the class) have this variable, but that when you call the method (e.g. ), you only have to pass in two values. Why don’t we have to pass in the parameter? This phenomena is a special behavior of Python: when you call a method of an instance, Python automatically figures out what should be (from the instance) and passes it to the function. In the case of , Python first creates and then passes it in. We’ll talk a little bit more about this below when we discuss the and methods.
We can also define methods to get the contents of the instance. The method takes an instance of a as a parameter and looks up the pet’s name. Similarly, the method takes an instance of a as a parameter and looks up the pet’s species. Again, we require the parameter so that the function knows which instance of to operate on: it needs to be able to find out the content.
As mentioned before, we don’t actually have to pass in the parameter because Python automatically figures it out. To make it a little bit clearer as to what is going on, we can look at two different ways of calling . The first way is the standard way of doing it: . The second, while not conventional, is equivalent: . Note how in the second example we had to pass in the instance because we did not call the method via the instance. Python can’t figure out what the instance is if it doesn’t have any information about it.
To drive the difference between instances and classes home, we can look at an explicit example:
This method is a special function that is defined for all classes in Python (you have have noticed that methods beginning and ending with a double underscore are special). You can specify your own version of any built-in method, known as overriding the method. By overriding the method specifically, we can define the behavior when we try to print an instance of the class using the keyword.
Let’s look at some examples of using the class!
Sometimes just defining a single class (like ) is not enough. For example, some pets are dogs and most dogs like to chase cats, and maybe we want to keep track of which dogs do or do not like to chase cats. Birds are also pets but they generally don’t like to chase cats. We can make another class that is a but is also specifically a , for example: this gives us the structure from but also any structure we want to specify for .
We want to specify that all s have species , and also whether or not the dog likes to chase cats. To do this, we need to define our own initialization function (recall that this is known as overriding). We also need to call the parent class initialization function, though, because we still want the and fields to be initialized. If we did not have line 4, then we could still call the methods and . However, because was never called, the and fields were never created, so calling or would throw an error.
We can define a similar subclass for cats:
A Closer Look at Inheritance
Let’s examine the difference between and .
The function used below, , is a special function that checs to see if an instance is an instance of a certain type of class. Here we can see that is an instance of , but not , while is an instance of both and :
Because is a , but not a , we can’t call on it because the class has no method. We can, however, call on , because it is defined for the class. Conversely, we can call the method on both and because they are both instances of , even though is not explicitly defined in the class.
Cats and Dogs
Now let’s create some cats and dogs.
Hopefully this was helpful in explaining what classes are, why we would want to use them, and how to subclass classes to create multiple layers of structure. For a more in-depth explanation of classes, you can look at the official Python documentation, or feel free to ask me any questions you might have. I would also be happy to more posts on other programming concepts – I’m open to suggestions!
Posted by Jessica Hamrickprogramming
« In Search of the Perfect Email SolutionThe Demise of For Loops »
- How do I access Plesk cPanel
- How is the Experis IT company
- How much does Shopify POS cost
- How many hours can an eagle fly
- What is mental treatment
- Can I meditate while smoking weed?no_redirect=1
- Which last name sounds the most prestigious
- Why do companies split their stocks
- Why do people believe in Islam
- What does philanderer mean
- Why is the abaya black in color
- What is Cloud ERP 1
- Why is Gautam Gambhir always ignored
- What about love in life
- Can I sue Facebook for blocking me
- Why was slavery needed besides labor
- Is perseverance the secret to accomplish something