9. Classes
A class serves as a blueprint or a template for creating objects. It bundles data (attributes) and functionality (methods) together into a single, logical unit
🡆Definition: Classes are defined using the class keyword, followed by the class name (conventionally in CamelCase) and a colon. The class body, including attributes and methods, is indented.
🡆Python Attributes: These are variables associated with the class or its instances.
Methods: These are functions defined within a class that operate on the object’s data.
Creating a new class creates a new type of object, allowing new instances of that type to be made
Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
Example
# define a class
class Dog:
sound = “bark”
#class attribut
Methods
Methods are functions defined within a class that operate on the instance’s data. They also take self as their first parameter.
class Dog:
def __init_(self,name ,age):
selff.name = name
# Instance attribute
A class is defined using the class keyword, followed by the class name (typically in CamelCase).
🡆The __init__ method is a special method called a constructor.
🡆 It’s automatically invoked when a new object (instance) of the class is created. It’s used to initialize the instance’s attributes.
🡆The self parameter refers to the instance itself.
Classes encapsulate data (attributes) and behavior (methods) related to a specific type of entity.
When we create a new class, we define a new type of object. We can then create multiple instances of this object type.
