When you have been working with Java for many years, it is sometimes fun to delve into other languages to get some new inspiration. For the past few days I have been meddeling around with Python / Django and Google App engine.
A discussion over MSN with a friend resulted in me coding this small example of how Python class and object variables differ greatly from what is normal in Java. Also this example illustrates very simple interitance from object->Vehicle->Car and Truck.
Code:
Package for vehicles:
'''
Created on Sep 10, 2009
@author: nikolaj
'''
class Vehicle(object):
'''
classdocs
'''
number_of_vehicles = 0
def __init__(self, color, number, weight):
self.number = number
self.weight = weight
self.color = color
Vehicle.number_of_vehicles += 1
print '(Initializing Vehicle %s which is of type %s)' % (self.number, type(self))
def __del__(self):
Vehicle.number_of_vehicles -= 1
if Vehicle.number_of_vehicles == 0:
print 'Vehicle: I am the last one on the streets now ...'
else:
print 'Vehicle: There are still %d of us left.' % Vehicle.number_of_vehicles
class Car (Vehicle):
'''
classdocs
'''
number_of_cars = 0
def __init__(self, color, number, passengers, weight):
Vehicle.__init__(self, color, number, weight)
self.passengers = passengers
Car.number_of_cars += 1
def __del__(self):
print 'Car: %s says bye. I am off to the scrap heap' % self.number
Car.number_of_cars -= 1
if Car.number_of_cars == 0:
print 'Car: I am the last Car on the streets now ...'
else:
print 'Car: There are still %d Cars left.' % Car.number_of_cars
def howmany (self):
print "Car: %d cars and %d vehicles in total" % (Car.number_of_cars, Car.number_of_vehicles)
class Truck (Vehicle):
'''
classdocs
'''
number_of_trucks = 0
def __init__(self, color, number, tonnage, weight):
Vehicle.__init__(self, color, number, weight)
self.tonnage = tonnage
Truck.number_of_trucks += 1
def __del__(self):
print 'Truck: %s says bye. I am off to the scrap heap' % self.number
Truck.number_of_trucks -= 1
if Truck.number_of_trucks == 0:
print 'Truck: I am the last Truck on the streets now ...'
else:
print 'Truck: There are still %d Trucks left.' % Truck.number_of_trucks
def howmany (self):
print "Truck: %d trucks and %d vehicles in total" % (Truck.number_of_trucks, Truck.number_of_vehicles)
Executable pyton main
from dk.hsys.model.vehicle import Car
from dk.hsys.model.vehicle import Truck
'''
Created on Sep 10, 2009
@author: nikolaj
'''
def main():
car = Car('Blue', 'RH20358', 4, 725)
car2 = Car('Yellow', 'DC79929', 2, 800)
car2.howmany()
truck = Truck('Red', 'CV34254', 22, 32)
truck2 = Truck('Green', 'BD7638', 2, 4)
truck3 = Truck('Black', 'HG76543', 30, 40)
truck.howmany()
if __name__ == '__main__':
main()
Output:
(Initializing Vehicle RH20358 which is of type )
(Initializing Vehicle DC79929 which is of type )
Car: 2 cars and 2 vehicles in total
(Initializing Vehicle CV34254 which is of type )
(Initializing Vehicle BD7638 which is of type )
(Initializing Vehicle HG76543 which is of type )
Truck: 3 trucks and 5 vehicles in total
Car: RH20358 says bye. I am off to the scrap heap
Car: There are still 1 Cars left.
Car: DC79929 says bye. I am off to the scrap heap
Car: I am the last Car on the streets now ...
Truck: CV34254 says bye. I am off to the scrap heap
Truck: There are still 2 Trucks left.
Truck: BD7638 says bye. I am off to the scrap heap
Truck: There are still 1 Trucks left.
Truck: HG76543 says bye. I am off to the scrap heap
Truck: I am the last Truck on the streets now ...