Saturday, 5 December 2015

Python: Get the doc string associated with a class

‘__doc__ ‘ is used to get the doc string associated with a class.

test.py
class Employee:
 """ Blue print for all employees """
 noOfEmployees=0  # Class level variable
  
 def __init__(self, id, firstName, lastName):
  self.id = id
  self.firstName = firstName
  self.lastName = lastName
  Employee.noOfEmployees = Employee.noOfEmployees + 1
  
 def displayEmployee(self):
  print(self.id, self.firstName, self.lastName)
  
emp1 = Employee(1, "Hari Krishna", "Gurram")
print("Total Employees", Employee.noOfEmployees)

emp2 = Employee(2, "PTR", "Nayan")
print("Total Employees", Employee.noOfEmployees)

emp3 = Employee(3, "Sankalp", "Dubey")
print("Total Employees", Employee.noOfEmployees)

emp1.displayEmployee()
emp2.displayEmployee()
emp3.displayEmployee()

print("Doc string is",Employee.__doc__)

$ python3 test.py 
Total Employees 1
Total Employees 2
Total Employees 3
1 Hari Krishna Gurram
2 PTR Nayan
3 Sankalp Dubey
Doc string is   Blue print for all employees



Previous                                                 Next                                                 Home

No comments:

Post a Comment