Object-Oriented Programming Fundamentals
1. Object-Oriented Programming (OOP) vs. Procedural Programming (POP)
Procedural programming: The core concept is procedure
Procedures refer to the steps for solving a problem. When programming, you first analyze the steps needed to solve the problem, implement these steps using functions, and then call these functions in sequence as you execute each step. This approach is suitable for simple tasks that don't require extensive collaboration.
Object-oriented programming: The core concept is object
Objects consist of attributes and methods. When programming with OOP, you think about how to design entities, such as how to draw a heart shape.
Real-world example:
- Purchasing a computer
- Procedural approach (buying yourself): Identify computer requirements, research specifications, compare models, compare prices, evaluate customer service, place order, receive delivery, verify upon startup, confirm receipt
- Object-oriented approach (having someone else buy): Identify computer requirements, find a professional to help select the computer
In summary, in programming, different approaches (procedural, object-oriented) aren't inherently better or worse. It's similar to cookbooks - different recipes appeal to different people, with no superior option. Similarly, different programming scenarios call for different approaches.
Classes and Objects
- Class: An abstract concept: a type in real life
- All variables in a class are called attributes
- All functions in a class are called methods
- Object: A specific instance of a class
Example of a cat:
- Cat attributes: Fur, tail, teeth
- Cat methods: Meowing, guarding home, running, eating
Class Definition
Defining a function:
def function_name(parameters):
function_body
function_name(arguments)
Defining a Class
class ClassName:
class_code(attributes, methods)
Class naming convention: Use PascalCase (capitalize the first letter of each word)
After defining a class, no instance objects exist: executing the code runs the code within the class
Variables defined in a class are called attributes; functions are called methods
class Person:
# Class attributes (shared by all objects)
# Later we'll learn instance attributes
name = "john"
age = 25
john = Person() # Instantiate an object john from the class
print(f'Name: {john.name}, Age: {john.age}')
Modifying Class Attributes
Dog.breed = "Golden Retriever"
Dog.breed = "German Shepherd"
Dog.breed = "Bulldog"
The __init__ Initialization Method
- The __init__ initialization method is also called an instantiation method, magic method, or constructor
- This method is automatically called when initializing an object, typically used to initialize object attributes
- This method is automatically called during object initialization and doesn't need to be manually invoked
self
Every method in a class automatically includes this parameter when defined. self represents the object itself.
Initialization Method
class Employee:
def __init__(self, name='default', age=30):
"""
Initialization method
:param name: Initial name
:param age: Initial age
"""
# self refers to the object itself
print("Initialization method called")
self.name = name
self.age = age
emp = Employee('alex', 35)
print(emp.name, emp.age)
print(emp.__dict__)
Instance Methods
class Employee:
def __init__(self, name='default', age=30):
"""
Initialization method
:param name: Initial name
:param age: Initial age
"""
# self refers to the object itself
print("Initialization method called")
self.name = name
self.age = age
def introduce(self):
"""
Introduction method - instance method
:return:
"""
print(f'Name: {self.name}, Age: {self.age}')
emp = Employee('alex', 35)
emp.introduce()