#!/usr/bin/python3 # -*- coding: utf-8 -*- class Employee: """Common base class for all employees""" empCount = 0 def __init__(self, name: str, salary: int) -> None: """ Special function auto-called when object created. Like a constructor in C++ (but not really). """ print("creating object now") # Instance variable access self.name = name self.salary = salary # This is just a local varible, that does nothing after this scope when # it is garbage collected! # name = name # salary = salary # Class varible access Employee.empCount += 1 # which is the same as this: # type(self).empCount += 1 # This would be a name error: # empCount += 1 # This is just a local varible, that does nothing after this scope when # it is garbage collected! # empCount = 4 def display_count(self) -> None: print("Total Employee %d" % Employee.empCount) def display_employee(self) -> None: print("Name : ", self.name, ", Salary: ", self.salary) def __str__(self) -> str: """ Special function called when print(obj) is called Enables object to be printed using print() This is better than the function above. Note: It's RETURNING not printing. """ return "Name : " + self.name + ", Salary: " + str(self.salary) def __del__(self) -> None: """ Special function auto-called when object deleted/garbage-collected. You should not generally actually use this function for anything. This is just to illustrate garbage collection here. """ print("takin out the trash") type(self).empCount -= 1 """ https://stackoverflow.com/questions/1481488/what-is-the-del-method-how-to-call-it You normally will not notice when the garbage collector destroys an orphaned instance and reclaims its space. But a class can implement the special method __del__(), like a destructor, that is invoked when the instance is about to be destroyed. This method might be used to clean up non-memory resources used by an instance. __del__ is a finalizer. It is called when an object is garbage collected which happens at some point after all references to the object have been deleted. __del__ is called when the garbage collector happens to be collecting the objects, not when you lose the last reference to an object and not when you execute del object. """ # Step into the constructors emp1: Employee = Employee(name="Bob", salary=2000) emp2: Employee = Employee("Jed", 5000) print(emp1.name) emp1.display_employee() # Step this. What runs? print(emp1) # which is the same as this: print(emp1.__str__()) emp2.display_employee() print(emp2) print("Total Employee %d" % Employee.empCount) emp1 = emp2 print("Total Employee %d" % Employee.empCount) del emp1 del emp2 """ Every Python class keeps following built-in attributes, and they can be accessed using dot operator like any other attribute − __dict__ − Dictionary containing the class's namespace. __doc__ − Class documentation string or none, if undefined. __name__ − Class name. (recognize this??) __module__ − Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. """ print("Employee.__dict__:", Employee.__dict__) print("Employee.__doc__:", Employee.__doc__) print("Employee.__name__:", Employee.__name__) print("Employee.__module__:", Employee.__module__) print("Employee.__bases__:", Employee.__bases__) # Do we see __del__ called for this object? emp3: Employee = Employee(name="Jane", salary=3000) # ++++++++++++++++ Back to slides for a bit