Guidelines

Class and Instance Example

In this lesson, let's explore the relationship between Class and Instance (or object) using a "bank account" as an example.

The code below defines a 'BankAccount' class and shows how to create multiple account instances to manage each account's unique attributes and behaviors.


Class Description

The BankAccount class represents a bank account and includes the following attributes and methods:

  • Attributes: owner, balance

  • Methods: deposit, withdraw, display_balance

The (__init__) is a constructor method called when an account instance is created from the class, setting the account object’s owner and initial balance.

The balance has a default value of 0 (balance=0).

BankAccount Class
class BankAccount: # Constructor method def __init__(self, owner, balance=0): self.owner = owner self.balance = balance # Deposit method def deposit(self, amount): self.balance += amount print(f"{amount} has been deposited.") # Withdraw method def withdraw(self, amount): if self.balance >= amount: self.balance -= amount print(f"{amount} has been withdrawn.") else: print("Insufficient balance.") # Balance inquiry method def display_balance(self): print(f"Account balance: {self.balance}")

Class Usage Example

Below is an example of using the BankAccount class to create account instances account1 and account2, and performing deposit, withdrawal, and balance inquiry for each account.

Class Usage Example
# Instance creation account1 = BankAccount("CodeFriends", 1000) account2 = BankAccount("GeekHouse", 2000) # Depositing, withdrawing, and checking balance for account1 account1.deposit(500) # 500 has been deposited. account1.withdraw(200) # 200 has been withdrawn. account1.display_balance() # Account balance: 1300 # Depositing, withdrawing, and checking balance for account2 account2.deposit(1000) # 1000 has been deposited. account2.withdraw(500) # 500 has been withdrawn. account2.display_balance() # Account balance: 1500

Each account (account1, account2) is an instance of the BankAccount class, having independent attributes (owner, balance) and methods (deposit, withdraw, display_balance).

Instances created from a class maintain their own data, and even if the same methods are used, different results are displayed for each instance.

For example, calling the display_balance method on the account results in a balance of 1300, while calling the same method on account2 results in a balance of 1500.


What is the self keyword?

In Python, self refers to the current instance within a class method.

When defining a method within a class, the first parameter is self, allowing the method to refer to the current instance.

The primary roles of self are as follows:

  • Accessing Instance Attributes: self is used to access and modify the attributes of the current instance within a method. For example, self.balance refers to the balance attribute of the current instance.

  • Method Calls: self is used to call other methods within the same instance. For example, self.deposit(amount) calls the deposit method within the same instance.

Mission
0 / 1

What is the most appropriate word to fill in the blank?

A class is a blueprint for creating .
instances
methods
attributes
functions

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result