Methods

What is a Method?

  • A method is just a function defined inside a class

  • It shows what a class can do, like behaviour or action

  • You always see self as the first argument (it means the method is linked to the object)

Example:

class BankAccount:
    def deposit(self, amount):  # this is a method
        print(f"Deposited {amount}")
  • deposit() is a method

  • It belongs to the BankAccount class

  • It defines what happens when someone deposits money


Types of Methods

  • Instance Method (default one)

  • Class Method

  • Static Method

  • Constructor


Types of Methods (based on logic)

Now, depending on whether the method has logic or just a rule, we call it:

  • Concrete Method

  • Abstract Method

Updated on