Introduction: How to Create and Use Methods in Python (using IDLE 3.8.3)

This instructional guide is made for students that require additional aid for using methods in Python, or simply for a user seeking to get a refresher on how methods work in Python.

With this in mind, in this guide I will assume that the reader understands the basics of coding, such as the following: how to return or print something, how to put commands into the Python Shell, and how to properly label your values.

Furthermore, in this guide, I will do the following: show the reader how to use methods in Python, present & explain vocabulary terms, and provide work examples.

Note 1: For this guide I will be using IDLE 3.8.3. With this in mind, there are no guarantees that this guide will be up to date with future versions of IDLE.

Step 1: Understanding the Basics

When creating classes, generally speaking we want to create an object that holds information.

For an example, lets assume that we want to create code in Python that can be used to label animals.

Well how can one differentiate animals from one another? Or better yet, how can we pin down an animal to be a ladybug?

  1. For one, animals can be narrowed down by their species.
  2. Another way animals can be narrowed down is by the type of food they eat (Carnivore, Omnivore, Herbivore).

In fact, the person writing the code can provide as many labels as they'd wish.

The take away from my example, is that when first creating a class, one will have to determine what information will be used to differentiate objects between each other.

Step 2: Creating the Class + Constructor

1. Creating the class - Use the following format to create a class in Python:

  • class InsertClassNameHere(inherited_class):

2. Creating the constructor

With the class set up, the first method that one will always follow up with is the constructor, which initializes or assigns values to the information inside our class.

To create the constructor we must use the __init__ method with our parameters, or variables of choice inside of the brackets.

  • E.g. def __init__(self, variableName1, variableName2):

Furthermore, we can also assign the default values of our parameters.

  • E.g. def _init__(self, variableName1 = 55, variableName2 = 'Test'):

Reminder: Refer back to our Animal example in Step #1 to decide on your variables of choice.

3. Inside the constructor

Inside of the constructor we must define our instance variables, variables that will serve as default values for every object created. This is done with the following format:

  • self.instanceVariableName = parameterOfChoice
  • Or, self.instanceVariableName = Manually put default value here

Note 2: All methods must start by being defined, which is done by using def followed by the method name.

Furthermore, when constructing all methods, the first parameter must be self. This is crucial as this is how we will refer to our objects.

Lastly, your instance variable names should not be the exact same as a parameter's name.

Step 3: Using the Method

At this point, we have a class with one method __init__. Before creating more methods, we must understand how __init__ works, and what we can do with it.

1. Creating an object - Use the following format to create an object in either the Python shell or the code itself:

  • objectName = ClassName()

2. Checking the data stored in said objects

With the instance variables, you can check the stored information in your object.

This is done by typing the following in the Python shell:

objectName.instanceVariableName

3a. Changing the values of an object's instance variable (without calling other methods)

If we don't want our object to be assigned its default value, we could change it while creating an object.

  • objectName = ClassName(insertValue, insertValue2) for as many parameters you've created.

Recall: My animal class has the parameters in the order of species, followed by dietType. Make sure that you put the proper values in your parameters in the correct order.

3b. Alternative way to edit the value of an object's instance variable

If you already created an object and wish to change it's values, you could type the following in the Python shell:

  • objectName.instanceVariableName = insertNewValue

Step 4: Creating More Methods

Now that I've shown you how to create a new object, call a method, and change the default values, we can start creating additional methods.

When creating additional methods, you have the choice of either returning some data, or editing said data.

1. Returning data

When creating a method that returns data, if we are not editing our values, then our parameters could simply contain only self.

  • e.g. def returnInfo(self):

Furthermore, inside the method we could simply return data with

  • return self.instanceVariableName

2. Editing data

When creating a method that edits data, in our parameter, after self, make sure reuse a parameter from __init__ of your choice.

  • e.g. def editVariable (self, parameterFrom__init__):

Furthermore, inside of the method we can change our instance variables with the following:

self.instanceVariableOfChoice = parameterFrom__init__

Step 5: Understanding "Child" Classes

Up to this point, we have created a class that contains methods and a set of variables for a given object.

With this in mind, we could refer to our current class as the parent class. This is because for my next step, I will show you how to create a child class.

When creating child classes, we intend to give them variables that are unique to them. In other words, the new variables created for an object that uses the child class cannot be accessed by an object of the parent class.

You may be asking yourself, when would I need to create and use child class?

We do so when we want to further label a specific type of object. For an example, connecting back to my Animal code, we can differentiate two tigers by their sex. With their sex, different characteristics and methods exist, such as only a female tiger could do the procedure of giving birth.

The take away from my example is that when an animal is a male, they have unique characteristics compared to females. Connecting this back to our idea of parent and child classes, if we create a child class for male animals, then we would create variables and methods unique to what a male can do (i.e. is the male a father?).

Step 6: Creating the Child Class + Methods

1. Creating the child class itself

When creating a child class, make sure that it is not inside of the parent class. I.e. make sure your child class is not indented inside the parent class like your existing methods.

Furthermore, when creating the child, we will reuse the format of the parent class with one exception, that being that the name of the parent class will be the parameter.

  • I.e. class childClassName(parentClassName):

2. Creating the constructor again

When creating a child class, we must once again create a constructor. In the constructor, we will reuse the parameters from the parent class, as well as create parameters unique to the child class.

With this in mind, the constructor of your child class should resemble the following:

  • def __init__(self, parameterFromParent1, parameterFromParent2, uniqueParameterToChild1): For as many parameters you have.

3. Inside the constructor

Rather than retyping instance variables that exist in our parent class, instead we could call the constructor from the parent class to reuse them. This is done inside the child class constructor with the following:

  • ParentClassName.__init__(self, parameters from parent class...)

Afterwards we can create instance variables unique to the child class with the format we've originally been using:

  • e.g. self.uniqueInstanceVariableToChild = uniqueParameterToChild

4. Create methods unique to the child class

When it comes to creating methods in the child class, truthfully it is no different than creating a method in the parent class. For this, we can reuse the formats from "Step 4: Creating More Methods."

Note 3: If we want to make our code efficient, our methods should be using the parameters and instance variables that are unique to the child class.

5. Understand that an object of the parent class cannot call a method from the child class

The last thing of importance that one should know about child classes is that for any methods or variables created for them, they cannot be reached by an object of the parent class.

As seen in the image above, b could call the method isFather() as b is an object of the child class "Male".

However, a is an object of the parent class "Animal," meaning that a cannot call isFather().

So, to summarize my point, an object of the parent class can't call a method from the child class.

However, an object of the child class can call a method from the parent class.

Step 7: Conclusion

At this point of the guide, I have shown you the basics needed to understand how to create and use methods in Python.

In fact, by this point the reader should be able to achieve the following: create classes, create methods, and use said methods.

Furthermore, at this point the reader should have gained a greater understanding of the vocabulary terms attached to the creation of methods, such as instance variables, parent + child classes, objects, etc.

With this guide, hopefully you could now finally work on your assignment that requires the use of methods, or perhaps now be able to use methods for an upcoming job interview that requires coding tests/quizzes.

As I've used an Animal class to demonstrate the use of methods, I have attached the code of said Animal class so you could use it yourself on IDLE.