Object Oriented Programming

Constructor in Python

A constructor is a special method that builds the object when a new object is created.


Syntax
class ClassName(object):
    def __init__(self, input_parameters):
        #set fields and run methods needed on instantiation here

Notes

The constructor is an instance method that is called implicitly on class instantiation. They are not considered members of a class.


Example
class Employee(object): 
    def __init__(self, employee, salary): 
        self.employee = employee 
        self.salary = salary

#instantiation
anthony = Employee("Anthony", "1MM")


See Also
Related

Class Methods

Documentation

Classes - Python Documentation

Add to Slack

< Class and Instance Variables   |   Instantiation >

© 2019 SyntaxDB. All Rights Reserved.