Welcome to CCP
Lets get started !
So, we completed with Error Handling in last part,
Till Now we have only talked about How to override and make methods in inherited classes or child classes,
what if we wanted to use the parent class version of a method or property in the overrided method or property in the child class ?
We can do it like this:
Notice the super().__init__() method which does almost half work for us by initializing age and now we have to do only name part to declare the name of the property,
similarly,
we can call super().<property-of-parent-class> to access the parent version of that property,
I hope you understood it,
Now lets move to static methods and class methods,
unlike normal methods, these methods are called by the whole class and not by the object,
To define a class method in python, we use @classmethod decorator and to define a static method we use @staticmethod decorator
example:
This example was about class methods, which are class-bounds and receive cls as their first parameter instead of self which refers to the Whole class,
Now, have look at static methods:
unlike classmethods, staticmethods receive neither cls nor self as arguments but can receive other general arguments,
these methods have a very general task to do like in this example, we just said the method to print Hello and can be called by anyone the class or its instances,
So these were the methods used in classes,
Hope you understood,
Up next, We will study about string formatting,
SEE YOU LATER !
Comments
Post a Comment