What are Objects?
In programming, an object is a self-contained entity that consists of both data and procedures to manipulate that data. Objects represent real-world entities, such as a bank account, a car, or a person.
What is Object-Oriented Programming (OOP)?
Object-Oriented Programming is a programming paradigm centered around objects. It organizes software design around data, or objects, rather than functions and logic. The key principles of OOP are encapsulation, abstraction, inheritance, and polymorphism, which we will explore in detail in later sections
Fundamental Building Blocks
Variables
Variables are the simplest building blocks in programming. They store data that can be used and manipulated.
# Example of variables
account_number = "001"
balance = 1000
Lists
Lists are ordered collections of items that can be of different types. They are mutable, meaning their elements can be changed.
# Example of a list
transactions = [100, -50, 200, -30]
Tuples
Tuples are similar to lists, but they are immutable, meaning their elements cannot be changed once assigned.
# Example of a tuple
account_info = ("001", "John Doe", 1000)
Dictionaries
Dictionaries are collections of key-value pairs. They are useful for storing related data and providing fast access to values based on their keys.
# Example of a dictionary
account_details = {
"account_number": "001",
"name": "John Doe",
"balance": 1000
}
Arrays
Arrays are similar to lists but are more efficient for numerical operations. They require importing the array module.
import array
# Example of an array
balances = array.array('i', [1000, 2000, 3000])
Building Objects in Python
To build objects, we start by grouping related data and behaviors. Without using OOP principles, we can use the built-in data structures to model objects.
Example: Bank Account without OOP
Using Dictionaries
# Representing a bank account using a dictionary
bank_account = {
"account_number": "001",
"name": "John Doe",
"balance": 1000,
"transactions": [100, -50, 200, -30]
}
# Functions to operate on the bank account
def deposit(account, amount):
account["balance"] += amount
account["transactions"].append(amount)
def withdraw(account, amount):
if amount <= account["balance"]:
account["balance"] -= amount
account["transactions"].append(-amount)
else:
print("Insufficient funds")
# Using the functions
deposit(bank_account, 500)
withdraw(bank_account, 200)
print(bank_account["balance"]) # Output: 1300
print(bank_account["transactions"]) # Output: [100, -50, 200, -30, 500, -200]
Using Lists and Tuples
# Representing a bank account using lists and tuples
account_number = "001"
name = "John Doe"
balance = 1000
transactions = [100, -50, 200, -30]
# Functions to operate on the bank account
def deposit(account_number, balance, transactions, amount):
balance += amount
transactions.append(amount)
return balance
def withdraw(account_number, balance, transactions, amount):
if amount <= balance:
balance -= amount
transactions.append(-amount)
else:
print("Insufficient funds")
return balance
# Using the functions
balance = deposit(account_number, balance, transactions, 500)
balance = withdraw(account_number, balance, transactions, 200)
print(balance) # Output: 1300
print(transactions) # Output: [100, -50, 200, -30, 500, -200]
Limitations without Using OOP
Using dictionaries, lists, and tuples to model objects can become cumbersome as the complexity of the program increases. It is harder to maintain and understand the relationships between different entities. This is where OOP principles come into play, offering a more structured and intuitive approach to organizing code.
Transitioning to OOP
By encapsulating related data and behaviors into classes, we can create more organized and maintainable code. Classes provide a blueprint for creating objects, enabling better abstraction, inheritance, encapsulation, and polymorphism.
In the next guide, we will delve into the advanced concepts of OOP in Python, exploring how these principles can be applied to create robust and scalable software solutions. Stay tuned!