In this tutorial, we will cover the following topics:
A class in Python is a blueprint for creating objects. It defines a set of attributes and methods that the created objects will have. Here's an example of a simple class and how to create an instance of it:
class Dog:
species = "Canis familiaris" # Class attribute
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age # Instance attribute
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
# Creating an instance of the Dog class
dog1 = Dog("Buddy", 9)
# Accessing attributes and methods using the instance
print(dog1.name) # Output: Buddy
print(dog1.age) # Output: 9
print(dog1.description()) # Output: Buddy is 9 years old
print(dog1.speak("Woof")) # Output: Buddy says Woof
A constructor is a special method that is automatically called when an instance of the class is created. In Python, the constructor method is called __init__.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
dog1 = Dog("Buddy", 9)
print(dog1.name) # Output: Buddy
print(dog1.age) # Output: 9
Methods are functions that are defined inside a class and are used to perform operations on the instances of the class. The first parameter of a method is always self, which refers to the instance calling the method.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
dog1 = Dog("Buddy", 9)
print(dog1.description()) # Output: Buddy is 9 years old
Both functions and methods are blocks of code that perform a specific task, but there are some differences between them:
def keyword outside of any class. They can be called independently.self parameter.Example of a function:
def greet(name):
return f"Hello, {name}"
print(greet("Alice")) # Output: Hello, Alice
Example of a method:
class Greeter:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, {self.name}"
greeter1 = Greeter("Alice")
print(greeter1.greet()) # Output: Hello, Alice
Inheritance allows a class to inherit attributes and methods from another class. The class that inherits is called a child class, and the class from which it inherits is called a parent class.
Example:
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
return f"{self.name} is eating"
class Dog(Animal):
def bark(self):
return f"{self.name} is barking"
dog1 = Dog("Buddy")
print(dog1.eat()) # Output: Buddy is eating
print(dog1.bark()) # Output: Buddy is barking
To create an instance of a class, you need to call the class using its name and pass the required arguments to its constructor. The created instance will have access to the class's attributes and methods.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
# Creating an instance of the Dog class
dog1 = Dog("Buddy", 9)
dog2 = Dog("Max", 5)
print(dog1.name) # Output: Buddy
print(dog1.age) # Output: 9
print(dog2.name) # Output: Max
print(dog2.age) # Output: 5
Once you have created an instance of a class, you can call its methods using the instance. To call a method, use the instance name followed by a dot and the method name with parentheses.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
# Creating an instance of the Dog class
dog1 = Dog("Buddy", 9)
# Calling methods using the instance
print(dog1.description()) # Output: Buddy is 9 years old
print(dog1.speak("Woof")) # Output: Buddy says Woof
Dog class may have attributes like name and age, and methods like description and speak.
__init__. It is used to initialize the instance attributes of the class.
self parameter.
dog1 = Dog("Buddy", 9) creates an instance of the Dog class with the name "Buddy" and age 9. The instance will have access to the class's attributes and methods.
dog1.description() calls the description method on the dog1 instance. This method will have access to the instance's attributes and other methods via the self parameter.