What is the use of self in Python

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Introduction:

  1. As we know that the Python is more powerful language which offer great tools for data crunching and preparation, as well as for complex scientific data analysis and modelling. 

  2. Python today has multiple implementations including Jython, scripted in Java language for Java Virtual Machine.

  3. IronPython is the new variety which is written in C# for the Common Language Infrastructure, and PyPy version written in RPython and translated into C. 

  4. Most of the Python modules work on community development model and are open-source and free.

What is the use of self in Python?

 

  1. In Python the self is used to represent the instance of the class. 

  2. Using this keyword, we can be able to access the attributes and methods of the class.

  3. It is also used for binding the attributes with the given arguments. 

  4. It is more often called as self because the Python does not use the '@' syntax to refer to instance attributes.

  5. Here in Python, if we need to access the methods which is used to passed the instances value automatically then we can use this technique.

  6. This method does not used to receive the values of the instances automatically.

Let us consider the following example below which will let you know about the use of self in detailed manner.

Here I am going to write the example which is best support for Python 3.

Example:

class car(): 

      

    # init method or constructor 

    def __init__(self, model, color): 

        self.model = model

 self.color = color 

          

    def show(self): 

        print("Model is", self.model ) 

        print("color is", self.color ) 

          

# both objects have different self which contain their attributes 

audi = car("audi a4", "blue") 

ferrari = car("ferrari 488", "green") 

  

audi.show()     # same output as car.show(audi) 

ferrari.show()  # same output as car.show(ferrari) 

Note: 

 

  1. It should be get noted that here Behind the scenes, in every instance method call, the Python is used to sends the instances also. 

  2. The Self is a convention and not a real python keyword. 

  3. The self is just a parameter in function like other parameters and if the user need then they are allowed to use another parameter name in place of it if they want.

  4. But as per the convention it is advisable and recommended  to use self because if we are going to use the self in the coding then it increase the readability of code.

Here I am going to write the example which is best support for Python 3.

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() 

 Python class self-Constructor:

As you know that the Python released version 3.0 which is also called as Python 3 on December 2008. Technically this version was mainly released in order to fix the major problems which are get found in Python 2. As you know that the Python 3 was incompatible with Python 2 in many aspects. It is backward incompatible and Some features of Python 3 have been backported to Python 2.x versions to make the migration process easy in Python 3.

  1. Constructors are generally used for instantiating an object.

  2. It is mainly used  to initialize(assign values) to the data members of the class.

  3. When an object of class is created then the members are to be get initialised using the constructor.

  4. In Python the __init__() method is called the constructor and is always called when an object is created.

Let us consider the following example which will let you know how the zip function is get applied in Python3.

Syntax:

def __init__(self):

    # body of the constructor

Example:

 

class Person: 

  

    # default constructor 

    def __init__(self): 

        self.name = "Smith"

   # a method for printing data members 

    def print_name(self): 

        print(self.name)

# creating object of the class 

obj = Smith() 

  

# calling the instance method using the object obj 

obj.print_name()

 

Is self in Python a Keyword?

As we have already discussed earlier above that the self is basically used in different places as a parameter to a function. But it is not a keyword.

Scope @ NareshIT:

  1. At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.

  2. Here you will get a good hand on practice in terms of practical industry-oriented environment which will definitely help you a lot to shape your future.

  3. During the designing process of application, we will let you know about the other aspect of the application too. 

  4. Our Expert trainer will let you know about every in’s and out’s about the problem scenario.

Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe on us and our advice, and we assured you about your sure success.