Posts

Showing posts from March, 2020

python super() in two mins.

Let's start with a simple class Contact that tracks the name and e-mail addresses of several people. The Contact class is responsible for maintaining a list of all contacts in a class variable, and for initializing the name and email of individual contact. class Contact (): all_contacts = [] def __init__ ( self , name , email ): self .name = name self .email = email Contact.all_contacts.append( self ) Now, we want to change the behavior of this class. Our Contact class only allows to add name and email address, but what if we also want to add a phone number? Well, one way is by creating a subclass that inherits the superclass, the newly created subclass method is automatically called instead of superclass's method. For Example : class Friend ( Contact ): def __init__ ( self , name , email , phone ): self .name = name self .phone = phone self .email = email Any method can be o...