Home > Back-end >  What is the point of using self, I am getting the same output
What is the point of using self, I am getting the same output

Time:05-14

#WITH INIT FUNCTION

class Details:
    def __init__(self,name,salary,role):
        print (f"The name is {name} and the salary is {salary} and role is {role}")

Dev = Details("DEV ",999,"PROGRAMMER")
Akash = Details("AKASH", 1000, "READER")


#WITHOUT INIT FUNCTION

class Details:
    def __init__(self,name,salary,role):
        self.name=name
        self.salary=salary
        self.role=role
        print (f"The name is {self.name} and the salary is {self.salary} and role is {self.role}")


Dev = Details("DEV MISHRA",999,"PROGRAMMER")
Akash = Details("AKASH PATHAK", 1000, "READER")

CodePudding user response:

self refers to the instance.

In the fist case, the attributes are not stored on the instance:

>>> Dev.name
AttributeError: 'Details' object has no attribute 'name'

In the second case, the attribute is stored on the instance.

>>> Dev.name
'DEV MISHRA'

This is important later if you want to access these attributes from instances or add additional methods to your class that need to know or modify these values.

class Details:
    def __init__(self,name,salary,role):
        self.name=name
        self.salary=salary
        self.role=role
    def give_raise(self, raise_amount):
        self.salary  = raise_amount

Dev = Details("DEV MISHRA",999,"PROGRAMMER")
Dev.salary
999
Dev.give_raise(100)
Dev.salary
1099

CodePudding user response:

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class. The name self is a convention. You can use any other name but always it has to be the first. Thus, it doesn't matter if you finally use it, but if you don't put it and use other parameters, the first one will be considered as your self.

In your example, if you delete self on your first method:

class Details:
def __init__(name,salary,role):
    print (f"The name is {name} and the salary is {salary} and role is {role}")

name will be the reference to the current instance of the class. Thus, if you call the method, you can only use salary and role parameters.

CodePudding user response:

If you write

class Details:
    def __init__(self,name,salary,role):
        print (f"The name is {name} and the salary is {salary} and role is {role}")

The variables name, salary and role exist only when the __init__() is being called, which is when you create the instance, aka. when you call Dev = Details(...). That's because they are arguments of a function

After that is done, they no longer exist and you can't access them later.

Writing self.name = name will "store" the name argument into an attribute of the instance you just created, meaning that it will be saved after the __init__ function is done processing. And you can access that through Dev.name.

CodePudding user response:

Maybe you should read into what object-oriented-programming is to understand what member variables are: https://realpython.com/python3-object-oriented-programming/

  • Related