In Python, classes are used to bring related data and functions together. To create a class, use
In the following code, you can find a simple
Employee1 class. This class has one variable,
name. Variables defined in classes are called attributes.
After Employee1 class definition, an instance of Employee1 class is created and given name
# Class Definition class Employee1: """ A simple Employee class """ name = "George" # Test Code employee1 = Employee1(); # an Employee instance is created print(employee1.name) # George
The __init__() Method (Class Constructor)
Employee1 class had one attribute called name. Every instance created from Employee1 class access the same value for name attribute: George.
# Class Definition class Employee1: """ A simple Employee class """ name = "George" # Test Code employee1 = Employee1(); employee2 = Employee1(); print(employee1.name) # George print(employee2.name) # George
Usually, classes are used, so that each instance has its own data. To assign values to attributes of an instance separately, you can use a Constructor (
__init__() Method). In the sample code below: employeeGeorge instance has the name “George”; employeeJohn instance has the name “John”.
# Class Definition class Employee2: """ An Employee class with a Constructor """ number_of_employees = 0 # Class Attribute def __init__(self, name): # Constructor Method """ Constructor for Employee2 class """ self.name = name # Instance Attribute Employee2.number_of_employees += 1 # Test Code employeeGeorge = Employee2("George"); employeeJohn = Employee2("John"); print(employeeGeorge.name) # George print(employeeJohn.name) # John print(employeeGeorge.number_of_employees) # 2 print(employeeJohn.number_of_employees) # 2
When an attribute is defined outside the
__init__() method in a class, this attribute is called a Class Attribute. All instances share common data for class attributes. In the example, number_of_employees is a class attribute. It has the same value (2) for both instances (employeeGeorge, employeeJohn).
When an attribute is defined inside
__init__() method, this attribute is an Instance Attribute. Every object has its own copy of the instance attribute. In the example, name is an instance attribute. The instance employeeGeorge has its own name “George”, the instance employeeJohn has its distinct name “John”.
Similar to Instance Attributes, Python classes can also have Instance Methods.
You can access attributes of an instance using
self keyword inside methods. The keyword
self refers to the current instance. For example, self.name is “George” for employeeGeorge instance, self.name is “Paul” for employeePaul instance.
Instance methods should have
self as the first method argument.
# Class Definition class Employee3: """ An Employee class with Constructor and an Instance Method """ def __init__(self, name, employeeId): """ Constructor for Employee3 class """ self.name = name self.employeeId = employeeId def print_employee_info(self): print("Employee Name is: " + self.name) print("Employee Id is: " + str(self.employeeId)) # Test Code employeePaul = Employee3("Paul", 54112) employeePaul.print_employee_info() # prints following lines: # Employee Name is: Paul # Employee Id is: 54112