python类中的特殊方法: __str__(self)用来重写
people类:
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)
输出:
peopleuser_id: 1 user_name: alan
peopleuser_id: 2 user_name: dream
peopleuser_id: 3 user_name: zuora
People 销毁
People 销毁
People 销毁