1. Home
  2. Docs
  3. Object Oriented Programming in Python
  4. Instance Methods

Instance Methods

What is an instance method ?

Instance method – A method that is called on an instance of a class. Instance methods are defined inside class body.

Instance method is function which takes a reference of the instance as the first argument.

Syntax:

class ClassName(object):
     
     def a_instance_method(self):
          statements

In previous examples we seen ‘__init__’ method,

which is special instance method, used for initializing the object.

Another common use of instance method is to access the class attributes.

The attributes defined in classes are accessible to its objects.

As we have seen in earlier section, attributes can be accessed by dot notation.

Recall our first class

class MyClass(object):

    x = 10
    
#create an instance 

m1 = MyClass()


#print the value x

print(m1.x)


output:

10

As Object Oriented Programming, promotes encapsulation and data hiding,

it’s not a good practice to access class attributes like this.

To access the attributes you will implement getters and setters known as accessors.

Example :

class MyClass(object):

    x = 10

    def get(self):

        return self.x
    
    def set(self, value):

        self.x = value

# creating an instance 

m1 = MyClass()

# accessing attribute x with dot notation

print(m1.x)

# set new value with set() instance method

m1.set(78)

# access the attribute x with get() instance method

print(m1.get())

output:
10

78

 

Instance methods are accessed through instance as below

instance.method() 

Notice when instance method is called, self is not passed to it,

its only required when you define instance method.

Python automatically passes the reference of instance as the first argument, i.e self.

self is just notation, if you’re from Java or C++ background, its similar to ‘this’

You can have this instead of self, or any valid identifier.

As self is automatically passed to methods, instance methods are known as ‘bound’ methods.

i.e method is bound to the instance upon which it is called.

What happens if you do’t pass self during method definition?

class MyClass(object):

    x = 10

    def get():

        return self.x
    
    def set(value):

        self.x = value

m1 = MyClass()

m1.set(78)

print(m1.get())


output:

TypeError                     Traceback (most recent call last)
<ipython-input-7-ca4f53d09c03> in <module>()
      9 m1 = MyClass()
     10 
---> 11 m1.set(78)
     12 print(m1.get())
     13 

TypeError: set() takes 1 positional argument but 2 were given

You’ll presented a TypeError, related to argument mismatch.

When you see this type error message, first thing do is to check,

whether you have passed self to instance method or not.

Let’s look at another example.

Below we are defining a Employee class, with first_name, last_name,

we have special __init__ instance method initializes the instance.

and full_name instance method which returns the full name of the employee.

class Employee(object):


    def __init__(self, first_name, last_name):

        self.first_name = first_name

        self.last_name = last_name

    def full_name(self):

        return '{} {}'.format(self.first_name,self.last_name)

# creating an instance 

emp1 = Employee("James","Tylor")

# creating another instance

emp2 = Employee("Anne","Holmes")

# call the instance method

# and print the return value

print(emp1.full_name())

print(emp2.full_name())


In the next lesson we will learn about class methods.