O'Reilly logo
  • Vlad Bezden thinks this is interesting:

class Employee(object):
numEmployee=0
def init (self,name,rate):
self.owed=0
self.name=name
self.rate=rate
Employee.numEmployee += 1

def del (self):
Employee.numEmployee-=1

def hours(self,numHours):
self.owed += numHours*self.rate
return ("%.2f hours worked" % numHours)

def pay(self):
self.owed=0
return("payed %s " % self.name)

From

Cover of Hands-On Data Structures and Algorithms with Python

Note

class Employee: numEmployee = 0

def __init__(self, name, rate):
    self.owed = 0
    self.name = name
    self.rate = rate
    Employee.numEmployee += 1

def __del__(self):
    Employee.numEmployee -= 1

def hours(self, numHours):
    self.owed += numHours * self.rate
    return "%.2f hours worked" % numHours

def pay(self):
    self.owed = 0
    return "payed %s " % self.name

e1 = Employee("John", 10) e2 = Employee("Smith", 20)

print(Employee.numEmployee) del(e1) print(Employee.numEmployee)