python class 1

//test.py

class Employee:
  'all employee'
  empCount = 0
  def __init__(self, name, salary):
    self.name = name
    self.salary = salary
    Employee.empCount += 1
  def prt(self):
    print 'self ', self
    print '__class__', self.__class__
  def displayCount(self):
    print 'Total Employee %d' % Employee.empCount
  def displayEmployee(self):
    print 'Name: ', self.name, ', Salary: ', self.salary

ee = Employee('zcl', 0)
print 'doc', Employee.__doc__
ee.displayCount()
ee.displayEmployee()
ee.prt()
print 'hasattr', hasattr(ee, 'empCount'), ee.empCount
print 'getattr', getattr(ee, 'empCount')
print 'setattr', setattr(ee, 'empCount', 2), ee.empCount
print 'delattr', delattr(ee, 'empCount')
print 'hasattr', hasattr(ee, 'empCount')

//result

# python test.py
doc all employee
Total Employee 1
Name: zcl , Salary: 0
self <__main__.Employee instance at 0x7f4564a60b48>
__class__ __main__.Employee
hasattr True 1
getattr 1
setattr None 2
delattr None
hasattr True

Finally:

这个例子告诉我们,类的属性是不能删除的

原文地址:https://www.cnblogs.com/woodzcl/p/7808782.html