Episode 18 of 29

Methods and Attributes

Deep dive into instance methods, class methods, and attribute management.

Let us explore methods (functions inside classes) and attributes (data inside classes).

Instance Methods

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

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

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient funds")

acc = BankAccount("Alice", 1000)
acc.deposit(500)
acc.withdraw(200)
print("Balance:", acc.balance)
Methods and Attributes — NoteArc Tutorials