To understand the need for creating a class, consider the following scenario: suppose you wanted to trace the number of fishes with different attributes such as breed and color. If you use a list, the first element could be the fish’s breed, and the second element could be its color.
How would you know which element is supposed to be which if there are 5000 different fishes? What if you wanted to give these fishes additional characteristics? This is disorganized, which is exactly what classes require.
Class is a user-defined prototype from which objects are created, and it serves as a container for data and functionality. Creating a new class creates a new type of object, allowing for the creation of new instances of that type. An instance of a class can be created and used to access and use the user-defined data structure that it creates, holding its own data members and member functions.
Key points for Python Class:
- Attributes are public and are accessed using the dot operator (.)
- Keyword class is used to create Classes
The syntax for Class:
class Class1:pass Here Class1 is the name of the class which is created
Objects are instances of a specific class. Every other element in Python, such as string, dictionary, number(10,40), and so on, will be an object of some class. Objects are different versions of the class that contain some actual values.
An object is made up of
- State – The attributes of an object determine the state of the object (i.e., the different items we have in the class, which the object inherits.).
- Behavior – The methods of the object represent the object’s behavior. It demonstrates the differences and similarities of an object’s functionality to that of other objects.
- Identity – Each object must be distinct in its own right.
After creating a class, the object is created. Object Instantiation is the process of creating an instance of an object with the same name as the class name. A newly created object can be given any name. Creating an object is similar to calling a function. This is because the default function Object() { } of the class is called automatically when the object is created or instantiated with the class name.
Code:
Python Code
class Fish:
name = "Nemo"
color = "blue"
age = 1 #age is considered in terms of month
Fish1 = Fish()
print(Fish1.name)
print(Fish1.color)
print(Fish1.age)
Output:
Nemo
blue
1
- _init_ method
Every time a new object is created from any class, the init method is called. The init method is similar to constructors in other programming languages (such as C++ and Java). This type of method contains statements that will be executed during run time (time of object creation). This method is denoted by a double underscore ( ).
- self parameter
The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class.
Code:
Python Code
class Fish:
# init constructor
def __init__(self, name):
self.name = name
# Method
def printName(self):
print('The Name of the fish is', self.name)
f = Fish('Nemo')
f.printName()
Output: The name of the fish is Nemo
An example of Python code that uses Classes, objects, self, and init
Code:
Python Code
class Fish:
animal = 'fish'
# The init constructor
def __init__(self, breed, color):
self.breed = breed
self.color = color
# Objects of Fish class
Nemo = Fish("Guppy", "blue")
Marlin = Fish("AngelFish", "orange")
print('Nemo details:')
print('Nemo is a', Nemo.animal)
print('Color: ', Nemo.color)
print('Breed: ', Nemo.breed)
print('\nMarlin details:')
print('Marlin is a', Marlin.animal)
print('Color: ', Marlin.color)
print('Breed: ', Marlin.breed)
Output:
Nemo details:
Nemo is a fish
Color: blue
Breed: Guppy
Marlin details:
Marlin is a fish
Color: orange
Breed: AngelFish
Special thanks to Shreyas Vishwakarma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article