MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Self & __init__() (Constructors)


Method:


A method is just like a function, with a def keyword and a single parameter in which the object's name has to be passed. The method's purpose is to show all the details related to the object in a single go. We choose variables that we want the method to take but do not have to pass them as parameters. Instead, we have to set the parameters we want to include in the method during its creation. Using methods makes the process simpler and a lot faster.

Self keyword:


The self keyword is used in the method to refer to the instance of the current class we are using. The self keyword is passed as a parameter explicitly every time we define a method.

def read_number(self):
        print(self.num)

__init__ method:-


"__init__" is also called a constructor in object-oriented terminology. Whereas a constructor is defined as:

"Constructor in Python is used to assign values to the variables or data members of a class when an object is created."

Python treats the constructor differently as compared to C++ and Java. The constructor is a method with a def keyword and parameters, but the purpose of a constructor is to assign values to the instance variables of different objects. We can give the values by accessing each of the variables one by one, but in the case of the constructor, we pass all the values directly as parameters. Self keyword is used to assign value to a constructor too.

As there can be only one constructor for a specific class, so the name of the constructor is a constant, i.e., __init__.

We declare a constructor in Python using the def keyword,

def __init__(self):
    # body of the constructor

Here,



The def keyword is used to define the function.
The first argument refers to the current object which binds the instance to the init() method.
In init() method ,arguments are optional. Constructors can be defined with any number of arguments or with no arguments.

Example


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

p1 = Person("John", 36)
print(p1.name) 
#Output: John

Types of constructors in Python


We have two types of constructors in Python.

The default constructor is the one that does not take any arguments.
Constructor with parameters is known as parameterized constructor.

Method vs. Function:


Methods and functions are very similar, yet there are some differences:

Methods are explicitly for Object-Oriented programming.
The method can only be used by the object that it is called for. In simple terms, for a method, the parameter must be an object.
The method can only access the data that is initialized in the class the method is formed in.

Example



class Employee:
    no_of_leaves = 8

    def __init__(self, aname, asalary, arole):
        self.name = aname
        self.salary = asalary
        self.role = arole

    def printdetails(self):
        return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"


harry = Employee("Harry", 255, "Instructor")


print(harry.salary)



Conclusion

In this page (written and validated by ) you learned about Python Self & __init__() (Constructors) . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Class Methods.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.