Methods

So objects have state. Cool.

However, if we want our objects to be like objects in the real world, we need to be able to do two things:

  1. Interact with our objects.

  2. Change the state of our objects. Think about it. Does anything in the real world truly stay the same forever? NOPE.

Computer scientists recognized these needs too, so they included a way to do both within their object oriented programs. We won’t talk about the specifics of the code needed just yet. All I want you to know is that we can satisfy both of these needs with something called methods.

A method is just a set of directions our computer can follow to change something about our object. In other words, a method is a function that changes the state of an object. If you don’t see the difference between these two terms, that’s ok; you can consider them synonyms in this class.

Most of the time, these methods will be applied to the objects that you create within your programs and will be defined by other programmers in the world; all you have to do is call the method you would like to use.

Still a little unsure of what this looks like to modify state with a method? Let’s go through an example. Imagine you want to makes a few movie objects. These are the attributes I would say all movies share:

Movie Attributes

Movies have…

  1. A director

  2. A genre

  3. A length

  4. A title

  5. A collection of actors

  6. Some amount earned to date

Here is an example movie object:

# End Game Object
director = 'Anthony Russo'
genre = 'Action'
length = 181
title = 'Avengers: End Game'
actors = ['Robert Downey Jr.', 'Chris Evans', 'Mark Rufflo', 'Chris Hemsworth', 'Scarlett Johansson', 'Jeremy Renner', 'Don Cheedle', 'Paul Rudd', 'Brie Larson', 'Bradley Cooper', 'Gwyneth Paltrow']
moneyEarned = 2800000000.00

Here’s the state of our object! Isn’t it nice? The state of this object is relatively steady too; we aren’t going to recast the movie now that it has been filmed after all. However, there are a few attributes that stand out to me as something we may want to interact with and change. In order to do this, we will need to use methods because, as I said above, methods allow us to operate on the internal state of our objects. Here are three methods that I will want to use:

Method Examples

Movies have…

  1. getGenre()

  2. convertMinsToHours(time)

  3. addToEarnings()

In this class, we will rely upon the methods that others have defined in this world for us to use. These ready-to-use methods come packaged together and can be easily used once you import them into your program. How do you import methods? You just write an import statement at the top of your program, like so:

import movies as mv

Here, we are importing a fictional library called movies and giving it the shorter name, mv. Python will find the library online and grab all of the methods that can be found in the library so that you can use them in your program. Instead of focusing on libraries that are not real, let’s examine a very real, and incredibly useful data science library called NumPy.