def count(): fs = [] for i in range(1,4): def f(): return i*i fs.append(f) print(fs[0]()) return fsf1,f2,f3 = count()print("-"*50)print(f1())print(f2())print(f3())print("-"*50)print(id(f1()))print(id(f2()))print(id(f3()))
#队列是为了线程安全,或则给文件加锁#FIFO# import queue# q = queue.Queue()## list1 = [1,2,3,4,5]## for i in list1:# q.put(i)## while not q.empty():# print (q.get())#LIFO 类似栈# import queue# q1 = queue.LifoQueue()## list2 = [3,5,7,8,0]## for m in list2:# q1.put(m)## while not q1.empty():# print(q1.get())from queue import PriorityQueueclass Job(object): def __init__(self, priority, description): self.priority = priority self.description = description print('New job:', description) return def __lt__(self, other): #operator < return self.priority < other.priorityq = PriorityQueue()q.put(Job(5, 'Mid-level job'))q.put(Job(10, 'Low-level job'))q.put(Job(1, 'Important job'))while not q.empty(): next_job = q.get() print('Processing job', next_job.description)