| by Arround The Web | No comments

Python Self Parameter

Python is an object-oriented programming language that allows you to define classes and create class objects. Classes are blueprints that contain attributes (data) and methods (functions) that belong to the objects. Objects are representations of classes that can interact with each other. To access the object attributes and methods within a class, the “self” parameter is used in Python.

This write-up presents a complete guide on the “self” parameter via the below-supported contents:

The Python “self” Parameter

In Python, the “self” parameter is a special keyword that is utilized to refer to the existing instance of a class. It is always the first parameter in a method definition and is utilized to access/retrieve the attributes and methods of the current/existing instance.

Note: The “self” parameter is needed because it allows us to retrieve the current object attributes and methods. By not including the “self” parameter, we would not be able to refer to the current object, and we would not be able to access/call its attributes and methods.

How to Use “self” in Python?

To use “self” in Python, we need to define a class and a function within the class that takes “self” as the first parameter. After that, we can use “self” to access/call the object attributes and methods.

Example 1: Basic Working of the Python “self” Parameter

The below code shows the working of the “self” parameter in Python:

class company:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def func1(self):
        print("Hello, my name is " + self.name)
p1 = company("Joseph", 15)
p1.func1()

 
In the above code:

    • The class with two attributes, “name” and “age”, is defined in the code.
    • The “__init__()” function is a special method that is accessed when an object is constructed/created. It is utilized to initialize the object attributes. The “self” parameter assigns the values of “name” and “age” to the object.
    • The “func1()” function is a regular method that accepts “self” as a parameter. It uses “self.name” to access the “name” object attribute.
    • The class object is created from the class, and the values for “name” and “age” are passed as arguments.
    • The “dot” notation is employed to call the “func1()” method via the created object.

Output


The function’s value has been displayed successfully.

Here are other examples of the “self” parameter to help us better understand its usage.

Example 2: Using “self” With Another Name

The “self” is not a keyword in Python; it is just a practice that most Python programmers follow. We can use any other name instead of “self” as long as it is the function’s first parameter:

class company:
    def __init__(cap, name, age):
        cap.name = name
        cap.age = age

    def func1(cap):
        print("Hello, my name is " + cap.name)
p1 = company("Joseph", 15)
p1.func1()

 
In the above code, the “self” is changed to “cap”, and this works exactly the same as before.

Output


The function value has been displayed successfully.

Note: However, using “self” is highly recommended as it makes your code more readable and consistent with other Python code. It also helps avoid confusion with other variables with similar names.

Example 3: Using “self” in Static Method and Class Method

The “Class” methods are the methods that take the class as the first argument in place of the instance. They are marked with the “@classmethod” decorator. They can be used to access or modify class attributes or to create new instances of the class.

The “Static” methods, however, don’t accept any parameters and don’t depend on the instance or the class. They are marked with the “@staticmethod” decorator. The class’s members can be used to perform some utility functions without accessing its state:

class calculation:
    x = 5.5
    y = 4.5
    @classmethod
    def circle_area(cls, radius):
        return cls.x * radius ** 2    
    @staticmethod
    def add(x, y):
        return x + y
print(calculation.circle_area(5))
print(calculation.add(3, 4))

 
In the above code lines:

    • The class “calculation” is defined with two class attributes named “x” and “y”. It also contains one class method and one static method.
    • The “cls” parameter is used to refer to the class itself. It is similar to “self”, but for class methods, we can use “cls.x” to access the class attributes.
    • The static methods don’t use “self” or “cls” parameters. They just perform some calculations using the arguments.
    • To call the class or the static methods, object creation is not required. We can just use the class name and dot notation.

Output

Conclusion

When referring to the current instance of a class, we utilize the “self” parameter to access the class’s variables and functions. The “self” is not a keyword and can be replaced by any other name, although it is not recommended. This article discussed Python’s “self” parameter using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply