Funders and magic methods

Why do we always use self as parameter for methods is it just because python considers everything as object

And difference between Init and constructor
Apart from naming :slight_smile:

Hey @Ramu-Rebal-2078248865781508, let me explain you in detail :

self represents the instance of the class. By using the โ€œselfโ€ keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.

The reason you need to use self is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on.

Please Note :
Self is a convention and not a real python keyword
self is parameter in function and user can use another parameter name in place of it.But it is advisable to use self because it increase the readability of code.

For example :

class this_is_class:  
    def show(in_place_of_self):  
        print("we have used another "
        "parameter name in place of self")  
          
object = this_is_class()  
object.show()  

init

โ€œinitโ€ is a reserved method in python classes. It is known as a constructor in OOP concepts. This method is called when an object is created from the class and it allows the class to initialize the attributes of a class.

I hope this clears your doubt and helps you understand the concept :slight_smile:
Happy Learning :slight_smile:

I hope Iโ€™ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.