Understand the different types of methods and how to use them in this Python Class A is a function embedded inside a . method class must have at least one parameter. There is no such thing as a parameterless . Typically is used as a parameter in that don't need any custom parameters. refers to the for which the is called and makes the and available for the method. Using the word is a norm that is typically followed. So even though can be replaced with any other word, doing so will add unnecessary surprises. A Method method self methods self object method object's variables methods self self Case Let's assume that from our successful delivery of the first simple project on creating a list with the ability to add and remove elements, we have got a new customer. He wants a similar list but with a slightly requirement - he wants to restrict the type of elements only to numeric types. strict Since the basic requirements are still the same: It's a list, It should be able to add and remove elements, And it should show the entire list. We will re-use the class that we created earlier and build on that using a called . Inside we can see two have been defined: , and . More about this is provided later. NumList subclass StrictNumList StrictNumList methods __check_value() add_value() lass NumList: def __init__(self, name = ""): self.instName = name self.__list = [] def add_value(self, val): self.__list.append(val) def remove_value(self): rv = self.__list[-1] del self.__list[-1] return rv def get_list(self): return self.__list class StrictNumList(NumList): def __init__(self, name): super().__init__(name) def __check_value(self, val): return str(val).isalpha() def add_value(self, val): if not self.__check_value(val): super().add_value(val) else: input_val = input("Insert a number: ") val = float(input_val) super().add_value(val) sl01 = StrictNumList(name = "Number List B1") sl01.instName sl01.add_value(2) sl01.add_value("abc") Insert a number: 5 sl01.get_list() [2, 5.0] Constructor - A Special Method or Python is a special kind of that's used to initiate a . __init__ constructor method class It runs every time the is instantiated and makes the usable. class class It must have at least one parameter - to make properties of the available. self object Unlike other methods, can't return a value or can't be invoked inside or outside of a . For example, you can't create call a method like < >< > . __init__ class object name . __init__() 2 Types of Methods Like , can be public or private, or better called private (remember ?). variables methods partially name mangling 1. Private Methods Naming a private follows the same pattern as the private - add two underscores ( ) before the method name. method variables __ Name mangling works with name the same way it works with the . class variables Why Use a Private Method? The same reason for using a private variable. The primary reason for creating these methods is to be used in other methods thus they don't need to be accessible directly from objects. But as we know from the last blog, name mangling actually makes them available. So using the mangled name we can actually access the private methods too. In our example class StrictNumList we have created as a private method. Because its main purpose is to be used inside the method. But check out the below code to see how we can still access this method using the mangled name! __check_value() add_value() # calling private method - __check_value() using its mangled name sl01._StrictNumList__check_value('5.5') False 2. Public Methods Public can be accessed directly from the using dot notation i.e. < >< >< >. In our example, is a public variable. methods object object name . method name add_value() 🛑 But notice how we have defined this method in both the and . This is called . super subclass method overriding Method overriding happens when you define the same method differently in the subclass. To use method overriding successfully you need to meet two conditions: It's only possible in the inheritance context. Or in other words, inheritance doesn't work inside the same . class method class The should have the same name and number of parameters as the . subclass superclass 🛑 Also notice, how the method from the was utilized in the method in . add_value() superclass add_value() subclass SuperNumList What's Next Till now in this series: OOP in Python We introduced and their basic building blocks, classes We learned about and its application of it using , inheritance Subclass We detailed and, in this post, . variables methods In the next post, we will try to get an insider view of the Python . And in doing so, we will see how learning about these topics will come in handy! Happy coding! exceptions