Parameter self unfilled python

Parameter Self in Python

The self parameter in Python is a reference to the instance of the class, which allows us to access the attributes and methods of that class. It is automatically passed as the first argument to the methods of a class when calling them.

Here is an example to illustrate the usage of the self parameter:


class Circle:
   def __init__(self, radius):
      self.radius = radius

   def get_area(self):
      return 3.14 * self.radius ** 2

# Creating an instance of the Circle class
my_circle = Circle(5)

# Accessing the attributes and methods using the instance and self parameter
print(my_circle.get_area())  # Output: 78.5
   

In the above example, we define a class Circle with an __init__ method which takes self (referring to the instance) and a radius parameter. We store the radius value in the self.radius attribute.

We also define a method called get_area which calculates and returns the area of the circle, using the self.radius attribute to perform the calculation.

After creating an instance of the Circle class, we can access the attributes and methods of that instance using the dot notation and the self parameter. In the example, we access the get_area method of my_circle and print the result.

Leave a comment