Saturday, 5 December 2015

Python: class: built in class attributes

Following are the built in class attributes.

Attribute
Description
__dict__
Dictionary contains class namespace.
__doc__
Points to documentation string of a class
__name__
Class name
__module__
Module name in which the class is defined
__bases__
Tuple representing the base classes of this 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)
  
print ("Employee.__doc__:", Employee.__doc__)
print ("Employee.__name__:", Employee.__name__)
print ("Employee.__module__:", Employee.__module__)
print ("Employee.__bases__:", Employee.__bases__)
print ("Employee.__dict__:", Employee.__dict__)

$ python3 test.py 
Employee.__doc__:  Blue print for all employees 
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: (<class 'object'>,)
Employee.__dict__: {'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Employee' objects>, 'displayEmployee': <function Employee.displayEmployee at 0x1006dd7b8>, '__init__': <function Employee.__init__ at 0x1006dd730>, 'noOfEmployees': 0, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': ' Blue print for all employees '}



Previous                                                 Next                                                 Home

No comments:

Post a Comment