Error Handling
Error Handling
Recommended Reading
Before reading this article, I recommend that you (should) read:
Blog Requirements
Python3
An IDE (See my other blog post for recommendations! – 10 Best Python IDEs for Windows)
What is a Class?
In Python, a class is a template for creating objects. It defines a structure for objects that belong to a
certain category or type. A class encapsulates data (attributes) and functions (methods) that operate
on that data. Objects created from a class are instances of that class, and they inherit the attributes
and methods defined in the class.
Attributes: Attributes are variables that store data within a class. They represent the
characteristics or properties of objects created from the class. For example, in a Car class,
attributes could include make, model, year, and color.
Methods: Methods are functions defined within a class. They define the behavior or actions
that objects of the class can perform. For example, a Car class might have methods like
start(), stop(), and accelerate().
Instance: An instance is an individual object created from a class. Each instance has its own
set of attributes and can invoke the methods defined in the class. For example, you can
create multiple Car instances, each representing a different car with its unique attributes
and behavior.
Constructor (__init__): The __init__ method is a special method in a class that is called when
an instance of the class is created. It is used to initialize the attributes of the object.
Encapsulation: Classes provide a way to encapsulate data and behavior into a single unit.
This helps in organizing and structuring code, as well as preventing unauthorized access to
data by making attributes private (by convention, using a single underscore prefix) and
controlling access through methods.
Example Of A Class
In the following example, Car is a class that has attributes (make, model, year, color) You can create
instances of the Car class, each representing a different car, and manipulate their attributes and
behavior.
class Car:
def __init__(self, make, model, year, color):
self.make = make
self.model = model
self.year = year
self.color = color
def __str__(self):
return f"Car Information: Make: {self.make}, Model: {self.model}, Year: {self.year}, Color:
{self.color}, Mileage: {self.mileage} miles, Running: {self.is_running}"
We now have to provide information to the class before we can print. We will create and assign a
variable to the values like the following:
Notice how we gave the class name and then inside the brackets, we provided a value to each
attribute.
Print(my_car)
class Car:
def __init__(self, make, model, year, colour):
self.make = make
self.model = model
self.year = year
self.color = colour
def __str__(self):
return f"Car Information: Make: {self.make}, Model: {self.model}, Year: {self.year}, Color:
{self.colour}
print(my_car)
The power of Classes however is that you can provide multiple results by using the same class.
You could have two cars for example.
Credits:
I like my titles to be tidy.
https://capitalizemytitle.com/style/Chicago/
I also like my grammar and spelling to be in check.
https://www.grammarcheck.net/editor/