OOP: Classes and Objects
3. The
4. The
What is
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Lesson 1 of 1
Python Classes Tutorial – Create, Use & Master Object-Oriented Programming (OOP)
Learn how to create and use classes in Python with practical examples. Master constructors, methods, attributes, and OOP principles for efficient coding.
Contents:
-
Introduction to Classes & Objects
- What is a class? (Blueprint for objects)
- What is an object? (Instance of a class)
-
How to Create a Class
- Basic syntax:
class MyClass: - Naming conventions (PascalCase).
- Basic syntax:
-
The
__init__Method (Constructor)- Purpose: Initialize object attributes.
- Syntax:
def __init__(self, ...)
-
The
selfKeyword (Explain early—it’s everywhere!)- What is
self? (Reference to the instance). - Why it’s needed (access attributes/methods).
- What is
-
Instantiating Objects
- How to create an object:
obj = MyClass(). - How
__init__is called automatically.
- How to create an object:
-
Adding Attributes to a Class
- Instance attributes (per-object, e.g.,
self.name = name). - Class attributes (shared, e.g.,
class_var = 0). - Class vs. Instance Variables (Compare them here).
- Instance attributes (per-object, e.g.,
-
Defining Methods in a Class
- What are methods? (Functions inside a class).
- Example:
def greet(self): print("Hello!").
-
Passing Arguments to Methods
- Methods with parameters:
def set_age(self, age):. - Default arguments (e.g.,
def __init__(self, name="Anonymous")).
- Methods with parameters:
-
Practical OOP Examples (Apply all concepts!)
- Example 1: A
Dogclass withname,breed,bark(). - Example 2: A
Carclass withmake,model,year, anddescribe(). - Example 3: A
Studentclass withname,age,gradesimple - Example 4: A
BankAccountclass withbalance,deposit(), andwithdraw(). - Example 5: A
Studentclass withname,age,grade, and methods likeintroduce().
- Example 1: A
their name, age, and grade.
1. Introduction to Classes & Objects
Class
- A class is a blueprint for creating objects.
- Defines the structure (attributes & methods) that objects will have.
- Example: A
Carclass defines properties likecolor,model, and actions likedrive().
Object
- An object is an instance of a class.
- Created from the class blueprint.
- Example: A
Toyotacar (object) created from theCarclass.
2. How to Create a Class
Syntax
class ClassName:
# Attributes & methods go here - Naming Convention: Use PascalCase (e.g.,
MyClass,BankAccount).
3. The __init__ Method (Constructor)
Purpose
- Automatically called when an object is created.
- Used to initialize attributes (set initial values).
Syntax
def __init__(self, param1, param2):
self.attribute1 = param1
self.attribute2 = param2 - Example:
class Dog:
def __init__(self, name):
self.name = name # Initialize 'name' attribute 4. The self Keyword
What is self?
- Refers to the current instance of the class.
- Used to access attributes & methods inside the class.
Why is it needed?
- Without
self, Python wouldn’t know which object’s attributes to modify. - Example:
class Dog:
def __init__(self, name):
self.name = name # 'self.name' belongs to this Dog object 5. Instantiating Objects
How to Create an Object
object_name = ClassName(arguments) - Example:
my_dog = Dog("Buddy") # Calls __init__ automatically 6. Adding Attributes to a Class
Instance Attributes
- Unique to each instance (object) of a class.
- Store data specific to that object.
- Defined within the
__init__constructor method, using theselfparameter.
class Dog:
def __init__(self, name):
self.name = name # Instance attribute Class Attributes
- Shared by all objects of the class.
- Defined outside
__init__.
class Dog:
species = "Canine" # Class attribute (shared) Class vs. Instance Attributes
| Feature | Class Attribute | Instance Attribute |
|---|---|---|
| Scope | Shared by all objects | Unique per object |
| Defined | Outside __init__ |
Inside __init__ |
for more details, see Understanding Class and Instance Attributes in Python
7. Defining Methods in a Class
What are Methods?
- Functions defined inside a class.
- Must take
selfas the first parameter.
Example
class Dog:
def bark(self):
print("Woof!")
my_dog = Dog()
my_dog.bark() # Output: "Woof!" 8. Passing Arguments to Methods
Methods with Parameters
class Dog:
def set_age(self, age):
self.age = age
my_dog = Dog()
my_dog.set_age(3) # Sets age to 3 Default Arguments
class Dog:
def __init__(self, name="Unknown"):
self.name = name
dog1 = Dog() # name = "Unknown"
dog2 = Dog("Buddy") # name = "Buddy" 9. Practical OOP Examples
Example 1: Dog Class
class Dog:
species = "Canine" # Class attribute
def __init__(self, name, breed):
self.name = name # Instance attribute
self.breed = breed
def bark(self):
print(f"{self.name} says Woof!")
# Create objects
dog1 = Dog("Buddy", "Labrador")
dog2 = Dog("Max", "Beagle")
dog1.bark() # Output: "Buddy says Woof!" Example 2: Car Class
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def describe(self):
print(f"{self.year} {self.make} {self.model}")
my_car = Car("Toyota", "Corolla", 2020)
my_car.describe() # Output: 2020 Toyota CorollaExample 3: Student Class
# Class Definition
class Student:
# Constructor
def __init__(self, name, age, grade): # self refers to the current object being created.
self.name = name
self.age = age
self.grade = grade
# Method
def info(self):
print(f"Name = {self.name} Age = {self.age} Grade = {self.grade}")
# Object Creation
student1 = Student("Hamza", 8, 3)
student2 = Student("Muhammad", 15, 10)
# Accessing Attributes and Methods
print(student1.name)
student1.info()
student2.info()Example 4: BankAccount Class
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print("Insufficient funds!")
# Usage
account = BankAccount(100)
account.deposit(50)
account.withdraw(30)
print(account.balance) # Output: 120 Example 5: Student Class
Represents a student with their name, age, and grade.
class Student:
def __init__(self, name, age, grade):
"""Initializes a Student object with the given attributes."""
self.name = name
self.age = age
self.grade = grade
def get_name(self):
"""Returns the student's name."""
return self.name
def get_age(self):
"""Returns the student's age."""
return self.age
def get_grade(self):
"""Returns the student's grade."""
return self.grade
def set_grade(self, new_grade):
"""Updates the student's grade."""
self.grade = new_grade
def introduce(self):
"""Prints a self-introduction message."""
print("Hello, my name is", self.name, "and I'm in grade", self.grade)
# Example usage
student1 = Student("Hamza", 8, 3)
student2 = Student("Muhammad", 16, 10)
student1.introduce() # Output: Hello, my name is Alice and I'm in grade 9
print(student2.get_name()) # Output: Bob
student2.set_grade(11)
print(student2.get_grade()) # Output: 11Python Tutorial in Urdu: How to Create a Class
Python Tutorial: Python Classes - What is Class Constructor
Python Tutorial in Urdu: How to Create Classes and Instance Attributes