Python Classes Guide
What Are Classes in Python?
Classes are a way to group data and functions together. They act as a blueprint for creating objects. If you think of an object as a real-world thing (like a car), then a class is the template that describes what the car looks like (its properties) and what it can do (its methods).
Creating a Basic Class
Here’s how you define a class:
class Dog:
# This is a class called Dog
pass
The pass
keyword means “do nothing.” This is useful when you’re setting up a class but don’t want to add anything to it yet.
Adding Properties and Methods
A property is a variable inside a class, and a method is a function inside a class.
class Dog:
# Initialize the object with some properties
def __init__(self, name, age):
self.name = name # Property: name
self.age = age # Property: age
# Define a method
def bark(self):
return f"{self.name} says Woof!"
Explanation:
__init__
method: This is the initializer. It runs when you create an object and sets up its properties.self
: Refers to the specific object being created or used.
Creating Objects
You can create objects (instances) from the class:
# Create a Dog object
my_dog = Dog(name="Buddy", age=3)
# Access properties
print(my_dog.name) # Outputs: Buddy
print(my_dog.age) # Outputs: 3
# Call methods
print(my_dog.bark()) # Outputs: Buddy says Woof!
Adding More Methods
You can add as many methods as you want:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says Woof!"
def birthday(self):
self.age += 1
return f"Happy Birthday, {self.name}! You are now {self.age} years old."
my_dog = Dog(name="Buddy", age=3)
print(my_dog.birthday()) # Outputs: Happy Birthday, Buddy! You are now 4 years old.
Example: A Simple Student Class
Here’s how you might represent a student using a class:
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def display_info(self):
return f"Student: {self.name}, Grade: {self.grade}"
def update_grade(self, new_grade):
self.grade = new_grade
return f"{self.name}'s grade has been updated to {self.grade}"
# Create a Student object
student1 = Student(name="Alice", grade="A")
# Use methods
print(student1.display_info()) # Outputs: Student: Alice, Grade: A
print(student1.update_grade("A+")) # Outputs: Alice's grade has been updated to A+
Class vs. Object
- Class: A blueprint (e.g.,
Dog
orStudent
). - Object: A specific instance of a class (e.g.,
my_dog
orstudent1
).