Classes revisited.
Python provides features that support object-oriented programming, including the ability to define new types or classes, which we saw in the POGIL on classes. It is not easy to provide a crisp definition of object-oriented programming, but we have already seen some of its characteristics:
- Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.
- Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.
Overview of OOP Terminology
- Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
- Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class’s methods. Class variables are not used as frequently as instance variables are.
- Instance variable − A variable that is defined inside a method and belongs only to the current instance of a class.
- Instance − An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
- Instantiation − The creation of an instance of a class.
- Method − A special kind of function that is defined in a class definition.
- Object − A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods.
To quickly review what we covered on class definitions in the POGIL, let us define a very simple Employee 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.
Creating Instance Objects
To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
Now, putting all the concepts together −
#!/usr/bin/python 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) "This would create first object of Employee class" emp1 = Employee("Zara", 2000) "This would create second object of Employee class" emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print( "Total Employee %d" % Employee.empCount)
When the above code is executed, it produces the following result −
Name : Zara ,Salary: 2000 Name : Manni ,Salary: 5000 Total Employee 2
You can add, remove, or modify attributes of classes and objects at any time −
emp1.age = 7 # Add an 'age' attribute. emp1.age = 8 # Modify 'age' attribute. del emp1.age # Delete 'age' attribute.
Activity
- Add a position attribute the the employee
- Add this position to the constructor and modify the code to include the position.
- Modify displayEmployee() to print this new attribute.
- Make the position attribute have a default value of “Intern” for the position
- Write a method called “giveRaise(percent), where percent is the percent raise to give. Thus “giveRaise(.10)” would give the employee a 10% raise. Test this out.
- Write code to add a code to automatically create a unique employee number called ID. Do this by using the current employee count as the ID.
- Modify the displayEmployee to show the employee number.
- Write a program that creates a list of employees. It gives three options: 1. Add Employee, 2. List Employees, 3. Delete Employee. Option 3 works by listing the employees, numbering them, and then asking you to enter the ID of the employee you wish to delete.
Another Class
Create another class called “Company” . Company has the following fields:
- CompanyName (string)
- Address (string)
- Employees (a list of employees)
And the following methods:
- __init__(slef, name, address)
- addEmployee(employee). This should prompt for the employee information, and create an employee and add it to the list.
- print() – show all the information about the company.
- fire(ID) – remove employee with ID.
- Write code to test it.
