Software & Finance





Python - Constructor __init__





 

I have given here python program explaining on how to write your first Python Class and a Constructor with __init__

This code is tested with Python version 2.6

 

        class CEmployee:
           age = 0
           name = ""
              
           def __init__ (self, name="unassigned", age=-1):
              self.name   = name
              self.age    = age
              
           def PrintRecord(self):
              print self.name, self.age
        
        
        s1 = CEmployee();
        s2 = CEmployee("Kathir", 33);
        s1.PrintRecord()
        s2.PrintRecord()

		#output
        #unassigned -1
        #kathir 33