Day 18: Defining Python Classes
Understanding Python Classes
In Python, classes are user-defined templates or blueprints that encapsulate data and functions into a single entity. They are defined using the 'class' keyword, followed by the class name and a colon. The attributes and methods of the class are defined in an indented block under the class definition.
The __init__
method is a special method in Python, known as a constructor. This method is automatically called when an instance of the class is created, and it is used to initialize the attributes of the instance. The 'self' keyword refers to the instance of the class and is used to access the attributes and methods of the instance from within the class.
Additional methods can be defined in the class by writing functions inside the class. These methods can operate on the attributes of the instance, providing the functionality associated with the class.
Defining a Class in Python
class Dog:
# Class attribute
species = "mammal"
# Initializer method
def __init__(self, name, breed):
# Instance attributes
self.name = name
self.breed = breed
# Method
def bark(self):
print("Woof!")
In the above example, a class named 'Dog' is defined with a class attribute 'species', two instance attributes 'name' and 'breed', and a method 'bark'.
Adding Additional Methods to a Class
Additional methods can be added to the class to provide more functionality. For example, a method 'bark_loudly' can be added to the 'Dog' class as follows:
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
print("Woof!")
def bark_loudly(self):
print("WOOF!")
dog = Dog("Fido", "Labrador")
dog.bark() # Output: "Woof!"
dog.bark_loudly() # Output: "WOOF!"
In this example, the 'bark_loudly' method is called on an instance of the 'Dog' class, and it prints "WOOF!" to the console.
Learn More
Want to learn more about Python for Machine Learning? Check out the full course HERE.
Need help mastering Machine Learning?
Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.