1 import hashlib 2 import time 3 4 def generate_md5(): 5 m = hashlib.md5() 6 m.update(bytes(str(time.perf_counter()),encoding='utf8')) #perf_counter() 可以精确到纳秒级别(保证不会重名) 7 return m.hexdigest() 8 9 class School(): 10 def __init__(self,s_id,name,addr): 11 self.id = s_id 12 self.name = name 13 self.addr = addr 14 def printInfo(self): 15 print("学校id:{}".format(self.id)) 16 17 class Course(): 18 def __init__(self,c_id,name,addr): 19 self.id = c_id 20 self.name = name 21 self.addr = addr 22 def printInfo(self): 23 print("课程id:{}".format(self.id)) 24 25 if __name__ =="__main__": 26 27 school = School(generate_md5(),"KAIYUAN","ShangHai") 28 course = Course(generate_md5(),"Python","Beijing") 29 30 school.printInfo() 31 course.printInfo()
注:可以作为文件名后缀。