53 Constructors and Other Special Methods | Python [ COMPLETE COURSE ]

Published: 21 August 2020
on channel: Tech Lab
62
1

Instead of creating classes with empty or default values, we can set these values when we create the instance. This ensures that we don't miss an important value and avoids a lot of unnecessary lines of code. To do this, we use a special method called a constructor.
When you call the name of a class, the constructor of that class is called. This constructor method is always named __init__. You might remember that special methods start and end with two underscore characters. In our example above, the constructor method takes the self variable, which represents the instance, as well as color and flavor parameters. These parameters are then used by the constructor method to set the values for the current instance.
In addition to the _init_ constructor special method, there is also the _str_ special method. This method allows us to define how an instance of an object will be printed when it’s passed to the print() function. If an object doesn’t have this special method defined, it will wind up using the default representation, which will print the position of the object in memory. Not super useful.

Christine Rafla
Email us at - techlaboffice77@gmail.com


On this page of the site you can watch the video online 53 Constructors and Other Special Methods | Python [ COMPLETE COURSE ] with a duration of hours minute second in good quality, which was uploaded by the user Tech Lab 21 August 2020, share the link with friends and acquaintances, this video has already been watched 62 times on youtube and it was liked by 1 viewers. Enjoy your viewing!