Python

Python Self Parameter

Self is a reference for any class. In Python, we will retrieve the class’s variables and operations by using the “self” keyword. It relates the provided parameters and the attributes. Python sometimes doesn’t employ the notation to link to class parameters, which is why we have to utilize self. The initial argument of functions in Python is the illustration the function is implemented on. Therefore, the example wherein the function corresponds is immediately provided but not received.

Example no 1:

The first argument of functions in Python that specifies an attribute is the string self. Hence, the user must employ self when calling a class’s variables and operations.

class Vegetable():

def __init__ (self, Vegetable,t):

self.vegetable_list = Vegetable

self.t = t

 

def show_t(self):

print("Total number of items:", self.t)

 

def show_basket(self):

print("Items included in cart:", self.vegetable_list)

 

def show_bill(self):

self.show_t()

self.show_basket()

my_b = Vegetable(["Onion", "Tomato", "Cabbage", "Potato", "Lady finger"], 80)

my_b.show_bill()

First, we will create a class named ‘Vegetables’. Then, we define the ‘self’ constructor of the class. Within the constructor, we set the attributes of the class which includes the vegetables and ‘t’. Here, ‘t’ show the total number of vegetables. Now, we want to show the total number of the item so we utilize the function show_total(). We pass the parameter ‘self’ to this function. The print() function is being called to show the total items.

Now, we will depict the list of vegetables. So, we utilize the show_basket() function and then the print() function. This print() method prints all the items in the cart. Furthermore, we are going to show the bill. For this, the show_bill() method is defined. Then, we call the show_t() function as well as the show_bassket() method. The objects are defined in the form of a list and are stored in the variable ‘my_b’. To terminate the program, we employ the billing function.

Example no 2:

In this instance, we will set both self and object, which relate to the same element.

class c:

def __init__(self):

print("Location of self = ",id(self))

object_of_class = c()

print("Location of object of the defined class = ",id(object_of_class))

We start this program by declaring the class ‘c’. Within this function, we define the function init(). This function contains the parameter of self. Then, we want to find the location of that parameter so we utilize the id() function inside the print() method. To display that location, we have been using the print() method.

Now, we create the object of the required class. In the next step, we obtain the location of the object so we apply the id() function for the object of the class. The print() method is being used to show the id of the object on the screen.

Example no 3:

Self can also indicate a static parameter that is part of the class. Let’s use an illustration to demonstrate how it operates.

class jeep():

 

def __init__(self, model, tint):

self.model = model

self.tint = tint

 

def show(self):

print("Model is", self.model )

print("Tint is", self.tint )

 

Slasher = jeep("Slasher c6", "Black")

Furiosa = jeep("Furiosa 654", "Sting Grey")

Slasher.show()

Furiosa.show()

After defining the class ‘jeep’, we will define the constructor of the class. We pass the model and tint as the arguments of this function. We set both attributes as the value of the self() function. Within the show function, we have been calling the print() function to show the model and color of the jeep.

Now, we define the model name and colors of the two jeeps. These two attributes have different self which contains arguments. The first name of the jeep is set as ‘Slasher’ and we define its model as ‘Slasher c6’ and color as Black. Similarly, the second jeep is ‘Furiosa’. Its model has specified as ‘Furiosa 654’ and its color is Sting Grey. In the end, we will display the details of both these jeeps so we are calling the show() function respectively.

Example no 4:

The Class function and constructor both require Self as a first argument. If we don’t specify it first, the program will raise an error.

class a:

def __init__():

print("It is a Constructor")

obj = a()

print("Runs successfully")

At the beginning of the code, the class named ‘a’ will be created. Then, we employ the init() method as a constructor. The print() method is being used to display the statement which shows that this method will be a constructor. Now, it’s time to create an object. Here we call the defined class. We will utilize the print() method to print the message which shows that the code runs successfully.

The name attribute for the whole “a” class is linked to “self” in the succeeding instance. Here, the self would not execute if an attribute is present inside a function. This attribute is exclusive to that function as it merely exists only when that function is operating. Global attributes or all of the class’s parameters must be specified outside of the functions.

Example no 5:

Self is a term that is used frequently in codes.  The user may substitute a different argument value for self, which is an attribute of the method.

class x:

def __init__(in_place_of_self):

print("Instead of self, we have utilized a different attribute name.")

 

object_of_class = x()

The class ‘x’ will be declared at the commencement of the program. Then, the init() function is used. We give this method the line “in place of self” as an argument. The statement “Instead of self, we have utilized a different attribute name” is shown using the print() function. After this print statement, a class object is created. Here, the specified class is called.

Conclusion

In this article, we have talked about the use of self parameter in Python. The user would need a technique to get at the class’s variables and operations once we define a class. We will give the self and object of the class the identical attribute. The self parameter is also used to specify the static variable to the object of the class. The constructor of the class contains the self as its first argument. By doing so, we get the error. In the function’s self argument, another argument name may be substituted by the user. However, employing self is recommended as it improves reusability.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content