Class Employee with class attribute count. : Attribute « Class « Python Tutorial






class Employee:
   count = 0       
   
   def __init__( self, first, last ):
      self.firstName = first
      self.lastName = last

      Employee.count += 1

      print "Employee constructor for %s, %s" % ( self.lastName, self.firstName )

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

      print "Employee destructor for %s, %s" % ( self.lastName, self.firstName )


print "Number of employees before instantiation is", Employee.count

# create two Employee objects
employee1 = Employee( "A", "C" )
employee2 = Employee( "B", "D" )
employee3 = employee1

print "Number of employees after instantiation is", employee1.count

# explicitly delete employee objects by removing references
del employee1
del employee2
del employee3

print "Number of employees after deletion is", Employee.count








11.2.Attribute
11.2.1.setattr and getattr
11.2.2.Attribute Reference
11.2.3.Modifying Class Attributes
11.2.4.Attributes are values associated with the object.
11.2.5.Instance Attributes versus Class Attributes
11.2.6.Use Caution When Accessing Class Attribute with Instance
11.2.7.Class Attributes More Persistent
11.2.8.Let us make our example more useful by giving P some attributes
11.2.9.Simple Attribute Lookup Example
11.2.10.hasattr(), getattr(), setattr(), delattr()
11.2.11.Class Employee with class attribute count.
11.2.12.Class that defines method __getattribute__ and __getattr__
11.2.13.Attach attributes to the class by assigning names to it outside of the original class