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

Pubblicato il: 21 agosto 2020
sul canale di: 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


In questa pagina del sito puoi guardare il video online 53 Constructors and Other Special Methods | Python [ COMPLETE COURSE ] della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Tech Lab 21 agosto 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 62 volte e gli è piaciuto 1 spettatori. Buona visione!