可以试试generator
http://www.python.org/dev/peps/pep-0289/from datetime import date
def number_generator(type_number):
last_day = date.today()
number = 0
while True:
today = date.today()
if last_day == today:
number += 1
else:
last_day = today
number = 1
yield '%s%s%03d' % (type_number, today.strftime('%Y%m%d'), number)
generator = number_generator('01')
print generator.next()
print generator.next()
print generator.next()
print generator.next()
结果:
0120110325001
0120110325002
0120110325003
0120110325004