Python Classes

  • Very similar to Java's
  • By default, eveything is public, and the parameters can be modified without a "getter" and "setter"
  • But also, new parameters can be added to instances on the fly

Let's do it in Java

public class Employee {

public String name;
public float salary;
public Date birth;

public Employee(String name, float salary, Date birth) {
    this.name = name;
    this.salary = salary;
    this.birth = birth;
}

public float GiveSalary(float percent) {
    this.salary *= (1 + percent/100.);
    return this.salary;
}

}

public class Dept {

public static int SizeOfCompany = 0;

public ArrayList <Employee> DeptEmployees;
public String dept_name;

public Dept(String dept_name) {
    this.dept_name= dept_name;
    this.DeptEmployees = new ArrayList <> ();
}

public void Hire(String name, float salary, Date birth) {
    Employee new_hire = new Employee(name, salary, birth);
    DeptEmployees.add(new_hire);
    SizeOfCompany += 1;
}

public void Fire(String name) {
    for (Employee a: DeptEmployees) {
       if (a.name == name) {
           DeptEmployees.remove(a);
           SizeOfCompany -= 1;
           break;
       }
    }
}

public Employee GetEmpl(String name) {
    for (Employee a: DeptEmployees) {
        if (a.name == name)
            return a;
    }
    return null;
}

}

In [1]:
# Let's do it in Python (roughly the same)

class Employee:
    
    def __init__(self,name,salary,date_of_birth):
        self.emp_name = name
        self.salary = salary
        self.date_of_birth = date_of_birth
    
    def GiveRaise(self,percent):
        self.salary *= 1 + percent/100.
        return self.salary
    
class Dept:
    
    # This is a class variable, all departments will share it
    SizeOfCompany = 0
    
    def __init__(self,dept_name):
        self.DeptEmployees = {}
        self.dept_name = dept_name
    
    def Hire(self,name,salary,birthday):
        new_empl = Employee(name,salary,birthday)
        # new_empl is a reference (pointer) to an instance of Employee
        self.DeptEmployees[name] = new_empl
        Dept.SizeOfCompany += 1
        
    def Fire(self, name):
        if name in self.DeptEmployees:
            del self.DeptEmployees[name]
            Dept.SizeOfCompany -= 1
            
    def GetEmpl(self,name):
        if name in self.DeptEmployees:
            return self.DeptEmployees[name]
        return None

Below, we'll do ordinary things in creating and deleting instances

Note that "SizeOfCompany" is independent of department -- it's a class variable shared by all instances of Dept

In [2]:
CS_Dept = Dept('Comp Sci')
CS_Dept.Hire('Fred',29999.95,'01/02/2003')
CS_Dept.Hire('George',40000,'04/05/2006')

TheGreatFred = CS_Dept.GetEmpl('Fred')
TheGreatFred.GiveRaise(15)

print ('After creating CS_Dept: %d people in the company' % Dept.SizeOfCompany)

Engl_Dept = Dept('English')
Engl_Dept.Hire('Harry',50000,'07/08/2009')
print ('After creating Emplish dept: %d people in the company' % Dept.SizeOfCompany)

CS_Dept.Fire('Fred')
print ('After firing Fred: %d people in the company' % Dept.SizeOfCompany)
After creating CS_Dept: 2 people in the company
After creating Emplish dept: 3 people in the company
After firing Fred: 2 people in the company

Now we'll access instance parameters without "getting" or "setting" them

In [3]:
Georgy = CS_Dept.GetEmpl('George')
print ('before: ',Georgy.salary)
Georgy.salary -= 15000
print ('after:  ',Georgy.salary)
before:  40000
after:   25000

Let's show some of the "looseness" of Python

Georgy has won an award. Let's note that in his instance, by giving him an "award" parameter and setting it to the name of the award. Note that ONLY GEORGY, amonst all employees, has an award parameter in addition to emp_name and salary. All other employees have only emp_name and salary.

In [4]:
Georgy.award = 'Nobel'
Harold = Engl_Dept.GetEmpl('Harry')

def ShowAward(employee):
    # Let's check first whether the employee has a parameter named "award" or not.
    if 'award' in employee.__dict__:
        print ('%s has %s' % (employee.emp_name,employee.award))
    else:
        print ('%s has no award parameter' % employee.emp_name)
        
ShowAward(Georgy)
ShowAward(Harold)

print (Georgy.__dict__)
print (Harold.__dict__)
    
George has Nobel
Harry has no award parameter
{'emp_name': 'George', 'salary': 25000, 'date_of_birth': '04/05/2006', 'award': 'Nobel'}
{'emp_name': 'Harry', 'salary': 50000, 'date_of_birth': '07/08/2009'}