Object Oriented Programming: Methods BEHIND THE SCENES!


Object Oriented Programming: Methods

Hi! Welcome to this second part of the Object Oriented Programming Tutorial.
In this section we are going to study very powerful concepts in Object Oriented Programming. METHODS!

Let’s get started!



Remember the example we worked with in the first part of the tutorial? If not, don’t worry, here we have the code to refresh your memory.
  • We covered naming the class, the __init__( ) function and what instances are and how to create them.
  • Now we will discuss what METHODS are.

The functions below __init__( ) must be familiar to you, they are exactly like function we have been working with, BUT notice that now, these functions are inside what we call a class.
This changes the scenario a little bit, but you must remember that they are still functions that can be called with an input, if necessary.
Let’s dive into METHODS!




  • When you create an instance, that instance will have access to all the methods the class has. The keyword self will store a reference to the instance so the instance will be able to call these functions and these functions can act on the its own individual attributes to modify them.
BUT if you call a method for a specific instance and that method modifies an attribute, it will only modify the instance’s attribute, not all the instances attribute.

You can read the code inside methods exactly like you would read normal functions code, the only difference is that their functionality changes a little bit because they belong an instance of a class.

We can access and change an instance’s attribute with a method by referring to it using self.attributeName



Calling Methods

Now, let’s discuss how to call methods.
Remember that they are functions? Well, functions are not very useful if we can’t call them, so in this case we have a way of calling functions that belong to a class.
  • We use instanceName.methodName( )

The parenthesis is VERY IMPORTANT because it means that we want to RUN the method instead of returning the method itself, the method definition.



NOTE: Yes, If you’ve noticed we have a talking house, you’re not crazy, we do have a talking house hahaha (one gets really creative with these examples, right? :-) )

Examples

To demonstrate what I mean with every instance having access to the class methods but that methods act on the instance's individual attributes, here we have two instances of the class House.

  • We first check to see that their attribute clean is the same, since all houses are initially clean according to our class definition.

  • Then, we call the instance house1 calls the method unCleanHouse. The method changes the attribute self.clean to the opposite Boolean


  • Now we check the clean attribute again on both instances, and now we notice that the attribute has changed for the instance that called the method unCleanHouse.


BUT that same attribute for house2 has remained intact.
Calling the method on house1 had no effect on house2 because each one has access to methods that act on their own individual attributes and don’t affect any other instances.

There are other types of methods such as Static Methods and Class Methods. If you'd like to learn more about them:

Hope it helps!
If you have any questions, don’t hesitate to ask, community TAs and your classmates will be there to help you in the forums.
Estefania.

Comments

Post a Comment