模板代码:
class People:'所有员工的基类'#构造函数def __init__(self, user_id, user_name):self.user_id = user_idself.user_name = user_name#析构函数def __del__(self):class_name = self.__class__.__name__print(class_name, '销毁')def get_info(self):print("people: ", "name = ," + self.user_name, "id = ", self.user_id)def __str__(self):return "people\n user_id: %d \t user_name: %s "%(self.user_id, self.user_name)
测试代码:
from People import People
p1 = People(1, 'alan')
p2 = People(2, 'dream')
p3 = People(3, 'zuora')
people_list = [p1, p2, p3]
for people in people_list:print(people)
# setattr(p1, 'age', '23')
#对当个对象添加属性
# print("setattr: ", p1.age)
#对当个对象删除属性
# delattr(p1, 'age')
#返回属性值
# getattr(p1, 'age')
#判断属性值是否存在
# hasattr(p1, 'age')
输出:
peopleuser_id: 1 user_name: alan
peopleuser_id: 2 user_name: dream
peopleuser_id: 3 user_name: zuora
People 销毁
People 销毁
People 销毁