In Python, classes and objects are fundamental concepts in Object-Oriented Programming (OOP). Here's a breakdown of both:
A class is a blueprint or a template that defines the properties (attributes) and functionalities (methods) of objects.
class Dog:
# Attributes
breed = ""
age = 0
You can create multiple objects from a single class, each with its own unique set of attributes.
An object is an instance of a class. It's a concrete realization of the class definition.
my_dog = Dog()
my_dog.breed = "Golden Retriever"
The __init__ function, also known as the constructor, is a special method in Python classes used to initialize objects. It's automatically called whenever you create an object from a class.
The __init__ method is defined within the class definition. It takes a special argument self which refers to the newly created object itself.
Apart from self, you can define other parameters to receive values that will be used to initialize the object's attributes.
Inside the __init__ method, you can assign values to the object's attributes using the self keyword. These attributes are essentially variables that hold data specific to the object.
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
my_dog = Dog("Golden Retriever", 5)
print(my_dog.breed) # Output: Golden Retriever
print(my_dog.age) # Output: 5
The __str__ method in Python is a special method that defines how an object should be represented as a string. It's called whenever you use built-in functions like str(), print(), or formatting functions like f-strings on an object.
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
def __str__(self):
return f"Dog: {self.breed}, Age: {self.age}"
my_dog = Dog("Golden Retriever", 5)
print(my_dog) # Output: Dog: Golden Retriever, Age: 5
An object method in Python is a function that is defined within a class and is associated with objects of that class. Methods allow objects to perform actions or calculations specific to their data.
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
def bark(self):
print(f"Woof! I'm a {self.breed}!")
my_dog = Dog("Golden Retriever", 5)
my_dog.bark() # Output: Woof! I'm a Golden Retriever!
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
The self parameter in Python is a special argument used within object methods of a class. It refers to the current object instance on which the method is being called.
It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class.
The most straightforward way to modify an object's property is through direct assignment using dot notation (.). Set the property's name equal to the new value:
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
def bark(self):
print(f"Woof! I'm a {self.breed}!")
my_dog = Dog("Golden Retriever", 5)
my_dog.age = 6
my_dog.breed="Bulldog"
print(my_dog.breed)
You can define methods within a class that specifically handle property modification. These methods can perform additional logic or validation before updating the property's value:
class Dog:
def __init__(self, breed, age):
self.breed = breed
self.age = age
def set_breedname(self, breed):
self.breed = breed
def bark(self):
print(f"Woof! I'm a {self.breed}!")
my_dog = Dog("Golden Retriever", 5)
my_dog.set_breedname("Bulldog")
print(my_dog.breed)
You can delete properties on objects by using the del keyword:
del my_dog.breed
Garbage Collection: Python employs automatic garbage collection to manage memory. When an object is no longer referenced by any variables, Python automatically reclaims the memory it occupies.
del keyword (for explicit deletion): While garbage collection handles most cases, you can explicitly delete objects using the del keyword
This removes them from memory immediately, regardless of references. However, it's generally not necessary to rely on this heavily, as garbage collection is efficient.
del my_dog
Define a class Student with attributes name, age, and grade. Create two objects of this class and initialize them with appropriate values.
Write a method within the class Student to display the details of a student.
Instantiate a few Student objects and display their details.
Create a class Circle with attributes radius and pi_value (with a default value of 3.14). Write a method calculate_area to calculate and return the area of the circle.
Instantiate a Circle object with a radius of 5 and calculate its area.
Change the value of pi_value to 3.14159 and calculate the area of the circle again.
Create a BankAccount class with attributes like account_number (string), balance (float), and owner (string).
Define methods:
Create a ShoppingCart class with attributes like items (list) and total_price (float).
Define methods: