Classes & Objects
A class is a blueprint for creating objects. It defines the attributes (data) and methods (behaviors) that the objects of that class will use.
Defining a Class
To define a class, you use the class keyword followed by the class name.
class MyClass:
# Class attributes and methods go here
For example, here we’ve defined a class called Person with attributes and methods.
First, we create a new class named “Person”.
class Person:
Adding Attributes
Next, we define the attributes the class will use. To do this, we use the __init__() method. This is a special method called a constructor and is executed automatically when a new object of the class is created.
def __init__(self, name, age):
self.name = name
self.age = age
The self parameter refers to the instance...