Let’s go through the code first and then I will explain the use of Class Constructor in this program. If you’re new to this series, please check the last post in which we have explained class methods (functions).
So let’s get started!
Constructor exercise for practice. You can copy this code for practice.
class YouTuber:
def __init__(self, name, channel_name, subscribers=0):
self.name = name
self.channel_name = channel_name
self.subscribers = subscribers
def get_channel_info(self):
print(f"Name: {self.name}")
print(f"Channel_name: {self.channel_name}")
print(f"Total Subscribers: {self.subscribers}")
youtuber1 = YouTuber("Vijay Sharma", "KLIENT SOLUTECH", 10000)
youtuber2 = YouTuber("Amit", "Amit_Tech", 20000)
youtuber1.get_channel_info()
youtuber2.get_channel_info()
3 Key highlights of this program
- The code defines a YouTuber class with attributes and a method.
- Two instances of the class (youtuber1 and youtuber2) are created with specific values, and the subscriber’s parameter has a default value of 0.
- The get_channel_info method is called for each instance, printing information about the respective YouTuber, including the total number of subscribers.
Class: “class YouTuber” defines a class named YouTuber.A blueprint for creating objects. It defines a data structure and behavior that the objects of the class will have.
Constructor (init): A special method in a class that is automatically called when an object is created. It is used for initializing the attributes of the object. For example: def init(self, name, channel_name, subscribers=0): is the constructor of the YouTuber class. It initializes the attributes (name, channel_name, and subscribers) when an object of the class is created.
Self: ‘self’ is a reference to the instance of the class, allowing you to work with the attributes and methods of that instance within the class.
In the init method, self is used to set the attributes of the instance (self.name, self.channel_name, self.subscribers). When you call a method like get_channel_info, the self parameter allows the method to access the attributes of the specific instance.
Object: In the code above youtuber1 = YouTuber(“Vijay Sharma”, “KLIENT SOLUTECH”, 10000) and youtuber2 = YouTuber(“Amit”, “Amit_Tech”, 20000) create instances of the YouTuber class (youtuber1 and youtuber2), which are concrete realizations of the class.
Attributes: In the code: name, channel_name, and subscribers are attributes of the YouTuber class, representing characteristics of each instance.
Default Value: In code: subscribers=0 in the constructor provides a default value of 0 for the subscribers attribute if no value is explicitly provided when creating an object.
Method: In code def get_channel_info(self): defines a method named get_channel_info associated with the YouTuber class. It prints information about the YouTuber, utilizing the attributes of the instance. Read more about this here: Class Instance specific methods (self) in Python
Instance: In code youtuber1 and youtuber2 are specific occurrences or instances of the YouTuber class.
Print Function: In this code print(f”Name: {self.name}”), print(f”Channel_name: {self.channel_name}”), print(f”Total Subscribers: {self.subscribers}”) use the print inbuilt python function to output information to the console.
Watch this Python Constructor Video Tutorial in Hindi for practice
Found this helpful? Let others know!