python tutorial - Constructor | Python Constructors - learn python - python programming




Constructor:

  • A constructor is a special type of method (function) that is called when it instantiates an object using the definition found in your class.
  • The constructors are normally used to initialize (assign values) to the instance variables.
  • Constructors also verify that there are enough resources for the object to perform any start-up task.
  • A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type.
  • A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

Creating a constructor:

  • A constructor is a class function that begins with double underscore (_).
  • The name of the constructor is always the same __init__().
  • While creating an object, a constructor can accept arguments if necessary.
  • When you create a class without a constructor, Python automatically creates a default constructor that doesn't do anything.
  • Every class must have a constructor, even if it simply relies on the default constructor.

What is the use of constructor in Python?

  • A constructor is a special kind of method that Python calls when it instantiates an object using the definitions found in your class.
  • Python relies on the constructor to perform tasks such as initializing (assigning values to) any instance variables that the object will need when it starts.

Example:

  • Following is the example of a simple Python class −
class Employee:
   'Common base class for all employees'
   empCount = 0

   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary
  • The variable empCount is a class variable whose value is shared among all instances of a this class. This can be accessed as Employee.empCount from inside the class or outside the class.
  • The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
  • You declare other class methods like normal functions with the exception that the first argument to each method is self.
  • Python adds the self-argument to the list for you; you do not need to include it when you call the methods.

Let's take an example:

  • Let's create a class named ComplexNumber, having two functions __init__() function to initialize the variable and getData() to display the number properly.

See this example:

 python constructor
  • You can create a new attribute for an object and read it well at the time of defining the values. But you can't create the attribute for already defined objects.

See this example:

 constructor python

Related Searches to Python Constructors