MyInternships.in

Python OOP

Python Methods

A method is simply a function defined inside a class, and it always operates on a particular object through the self parameter.


Defining an Instance Method

Methods are indented inside the class body, just like __init__. The first parameter is always self, which represents the object the method was called on.

A class with two methods
Python
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def show_balance(self):
        print(f"{self.owner}'s balance: {self.balance}")

Calling Methods

You call a method on an object using dot notation. Python automatically passes the object as self, so you only supply the remaining arguments.

Using the methods
Python
acc = BankAccount("Sara", 1000)
acc.deposit(500)
acc.show_balance()
Output
Output
Sara's balance: 1500

Method vs Function

A regular function is independent and lives at the module level; a method belongs to a class and is called through an object, always receiving that object as its first argument automatically.

FunctionMethod
Defined outside a classDefined inside a class
Called directly: greet()Called on an object: obj.greet()
No automatic selfReceives self automatically
Not tied to any dataOperates on an object's attributes
💡

Behind the scenes, obj.method(x) is equivalent to ClassName.method(obj, x) — Python just writes the friendlier dot-notation version for you.

ℹ️

Instance methods are the most common kind of method; Python also has class methods and static methods, covered in a later topic.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships