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: https://replit.com/@JimSkon/EmployeeClass

  • 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, we can verify that the objects are created properly and the class variable is updated correctly.

emp1.displayEmployee()
emp2.displayEmployee()
print("Total employees:",Employee.empCount)

When the above code is executed, it produces the following result −

Name :  Zara ,Salary:  2000
Name :  Manni ,Salary:  5000
Total Employees 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 to the employee class
  • 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 code to add a new attribute called ID. It should be automatically created and be a unique for each employee. The ID should start at 3000, and increment by 3
  • Modify the displayEmployee function to show the employee number.
  • 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.

Another Class

Create another class called “Company” . Company has the following fields:

  • CompanyName (string)
  • Address (string)
  • Employees (a list of employee objects)

And the following methods:

  • __init__(self, 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. (remove from list)
  • Write code to test it.

sol1

sol2

Scroll to Top