Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Coding your first Python Class.

Photo by Elisa Ventur on Unsplash

Recommended Reading
Before reading this article, I recommend that you (should) read:

Post Name: The Ultimate 9 Commands And Concepts in Python.

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.

Here are some key concepts related to Python classes:

 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.

First create a class:

class Car:
def __init__(self, make, model, year, color):
self.make = make
self.model = model
self.year = year
self.color = color

We then use the __str__ method to return an action.


By defining a __str__ method in your class, you can provide a custom string representation that
includes relevant information about the object's attributes. This makes it easier to understand and
debug your code because you can see the state of your objects when you print them.

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:

my_car = Car("Toyota", "Prius", 2021, "Blue")

Notice how we gave the class name and then inside the brackets, we provided a value to each
attribute.

We can then print the variable result by simply writing:

Print(my_car)

Your code should look like this.

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}

my_car = Car("Toyota", "Prius", 2021, "Blue")

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.

my_car = Car("Toyota", "Prius", 2021, "Blue")


mysecondcar = Car("Ford","Kapri",2021,"Green")

Want more information or just a chat?


You can find me on Twitter, Facebook, and LinkedIn or send me an email!

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/

You might also like