python class 1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
这个例子告诉我们,类的属性是不能删除的
以上是关于python class 1的主要内容,如果未能解决你的问题,请参考以下文章