Coder Social home page Coder Social logo

dsc-instance-methods-notredame-se-ds-preview's Introduction

Instance Methods

Introduction

Now that you know what classes and instances are, you can start to talk about instance methods. Instance methods are almost the same as regular functions in python. The key difference is that an instance method is defined inside of a class and bound to instance objects of that class. Instance methods can be thought of an attribute of an instance object. The difference between an instance method and another attribute of an instance, is that instance methods are callable, meaning they execute a block of code. This may seem a bit confusing, but try to think about instance methods as functions defined in a class that are really just attributes of an instance object from that class.

Objectives

You will be able to:

  • Understand instance methods as attributes
  • Define an instance method
  • Introduce self

Instance Methods as Attributes

When you think of methods and functions, you think about what kind of action they perform. The same goes for instance methods, however, the action being performed is scoped directly to that instance object. Remember, classes are kind of like the blueprints for their instance objects. So, let's take the example of a Dog class. What are the things that all dogs do? They can bark, beg to go for a walk, chase squirrels, etc. So, you consider these more or fewer attributes of a dog -- the same as their name, breed, weight, age etc. When you create a new dog instance object, the dog should be able to automatically bark, beg, and chase squirrels.

Let's see how you would create a single dog, rex, and get him to bark:

class Dog():
    pass 
rex = Dog()
rex
rex.bark()

Okay, here you have an instance of the dog class, but as you can see rex cannot bark yet... Let's see if you can fix that. You said instance methods are basically functions that are callable attributes, like functions, of an instance object. So, let's write a function that returns the string "bark!", and assign it as an attribute of a dog.

Note: Dictionary object attributes are accessed using the bracket ([]) notation. However, instance object attributes are accessed using the dot (.) notation.

def make_a_bark():
    return "ruff ruff!"

rex.bark = make_a_bark
rex.bark

Here you can see that you successfully added a the bark attribute to rex and assigned it the function make_a_bark. Note that the return value of rex.bark is simply a function signature since you have not yet executed the function, and although this looks like an instance method it is not quite.

Note: Although you may hear and see the terms method and function used interchangeably, there are slight differences. You know that function is essentially an object that contains a block of code and it can optionally take in data or objects as explicit parameters, operate on them, and optionally return a value. A method is, simply put, a function that is bound to a class or instances of that class. Instance methods, thus, are functions that are available/bound to instance objects of the class in which they are defined. However, a key difference between the two is that a method is implicitly passed to the object on which it is called, meaning the first parameter for the method is the object. Don't worry if this is confusing as you will dive more into this later.

rex.bark()

Great, now you see that once you called the bark attribute, you executed the function and now see our return value, "ruff ruff!".

This is a great first step. However, since make_a_bark is not actually defined inside our class, you are able to call it without our dog instance object, rex, and as you just covered, that's not really how instance methods work...

make_a_bark()

Alright, so, how do you turn this into a real instance method? Well, the first thing you need to do is define it inside of our class. So, let's take a look at how you can do that below:

Defining an Instance Method

class Dog():
    
    def bark():
        return "I'm an instance method! Oh and... ruff ruff!"
new_rex = Dog()

new_rex.bark

Here, you have re-defined our Dog class, but this time you actually defined an instance method bark. Now, whenever you create a new instance of this new Dog class, that instance will have the bark instance method as an attribute.

Notice that the signature that is returned by the unexecuted method says bound method instead of function, as was the case with our first rex's bark.

There is one issue with our instance method. Let's try calling it and see what happens:

new_rex = Dog()
new_rex.bark()

Uh oh! TypeError: bark() takes 0 positional arguments but 1 was given. This error is telling you that the method, bark was defined to take 0 arguments, but when you executed it, you gave it an argument.

Remember that one of the key differences between functions and methods is that a method is bound to an object and implicitly passes the object as an argument. That is what is causing this error. Effectively, what is happening when you try to call the instance method is this:

# the instance object, new_rex, is implicitly passed in as the first argument upon execution
new_rex.bark(new_rex)

So, how do you fix this error? Well, if instance methods will always require a default argument of the instance object, you will need to define our instance methods with an explicit first parameter.

Note: Parameters are the variable names you give to our method or function's future data. They are called parameters when you talk about the definition of a method or function, but once you have the data they are arguments.

def function_example(parameter1, parameter2):
    return parameter1 + parameter2
# since you are defining the function, the variables, parameter1 and parameter2, are called parameters

function_example("Argument1", "Argument2") 
# here the strings passed in, "Argument1" and "Argument2", are arguments since you are executing the function

Okay, so let's see if when you define our function with a parameter, you can get rid of the error. You'll also define another function who_am_i to help further understand what's happening here.

Introducing self

class Dog():
    
    def bark(self):
        return "I am actually going to bark this time. bark!"
        
    def who_am_i(self):
        return self
        
newest_rex = Dog()
newest_rex.bark()

Awesome! It works. Again, since instance methods implicitly pass in the object itself as an argument during execution, you need to define our method with at least 1 parameter.

Notice that the parameter is named self. As with any function or method, you can name the parameters however you want, but the convention in Python is to name this first parameter self, which makes sense since it is the object on which you are calling the method. If that sounds confusing, don't worry. The concept of self is a little confusing. Let's play around with it and see if you can get a better idea.

Let's first see what the return of the who_am_i method is:

fido = Dog()
print("1.", fido.who_am_i()) # checking return value of method
print("2.", fido) # comparing return of the fido instance object 

As you can see our who_am_i method is returning the same instance object as fido, which makes sense because you called this method on fido, and if you look at the method all it does is return the first argument (self), which you said is the instance object on which the method was called.

print("3.", fido == fido.who_am_i()) 
print("4.", newest_rex == newest_rex.who_am_i()) 
# again asserting that `self` is equal to the instance object on which who_am_i was called

Again, don't worry if self still seems a bit confusing. It will become clearer through practice and it will also be very useful as you get further into object-oriented programming. For now, you can just go forward with the knowledge that to define an instance method and later call it on an instance object, you will need to include at least one parameter in the method definition.

Summary

In this lab, you introduced a lot. You looked at instance methods and adding functions as attributes of objects. Then you looked at the differences between functions and instance methods. You learned that instance methods are bound to objects and they always use the object on which they are called as their first argument. Since instance methods use their object as an argument you looked at how to properly define an instance method by introducing the concept of self in object-oriented programming.

dsc-instance-methods-notredame-se-ds-preview's People

Contributors

loredirick avatar peterbell avatar mike-kane avatar mathymitchell avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Yoan Ante avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.