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

Veröffentlicht am: 21 August 2020
auf dem Kanal: 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


Auf dieser Seite können Sie das Online-Video 53 Constructors and Other Special Methods | Python [ COMPLETE COURSE ] mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Tech Lab 21 August 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 62 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!