Explain the init and str method with an example python program. (8 Marks)
The init method
Like the constructors in Java, the init method is a special method that gets called whenever a new object is created or instantiated. Its full name is __init__ (two underscore characters, followed by keyword init, and followed by two more underscores).
Video Tutorial
Let us take an example to understand the __init__ method. An init method for the Time class might look like this. The time class has three attributes Hour, Minute, and Second. As and when a new object of the Time class is created these three attributes are initialized. The different possible cases are shown below.
The init method for Time class
class Time: """Represents the time of day. attributes: hour, minute, second""" def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.minute = minute self.second = second def print_time(t): print('Hour:', t.hour, '\nMinute: ', t.minute, '\nSeconds: ', t.second) #The parameters are optional, so if you call Time with no arguments, you get the default values. print('----------------') time1 = Time() time1.print_time() #If you provide one argument, it overrides hour: print('----------------') time2 = Time(10) time2.print_time() #If you provide two arguments, they override hour and minute. print('----------------') time3 = Time(10,20) time3.print_time() #If you provide two arguments, they override hour, minute and seconds print('----------------') time4 = Time(10,20,30) time4.print_time()
Output:
---------------- Hour: 0 Minute: 0 Seconds: 0 ---------------- Hour: 10 Minute: 0 Seconds: 0 ---------------- Hour: 10 Minute: 20 Seconds: 0 ---------------- Hour: 10 Minute: 20 Seconds: 30
It is common for the parameters of __init__ to have the same names as the attributes. The statement self.hour = hour stores the value of the parameter hour as an attribute of the self.
As shown in the output, if you create a new object with no parameters, attributes Hour, Minute and Second are initialized to 0 and so on.
The __str__ method
like __init__ method, __str__ is also a special method, that returns a string representation of an object. For example, here is an str method for Time objects.
class Time: """Represents the time of day. attributes: hour, minute, second""" def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.minute = minute self.second = second def __str__(self): return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) print('----------------') time1 = Time() print(time1) print('----------------') time2 = Time(10, 20) print(time2) print('----------------') time3 = Time(10, 20, 40) print(time3)
Output:
---------------- 00:00:00 ---------------- 10:20:00 ---------------- 10:20:40
As and when the print function is called with primitive data like string, number, etc the values are printed normally. But when the print is called with object as the parameters, the __str__ function is called. The __str__ function returns the string form of the result. Finally, the print function displays the result.
Clik here to read Solution to Python Application Programming Question Paper Jan 2019 15CS664
If you like the tutorial share it with your friends. If you want to get the regular update regarding VTU CBCS notes, question papers, Interview preparation like our Facebook page.