#!/usr/bin/python3 # Copyright (C) 2014 Steve Litt, Expat license, http://directory.fsf.org/wiki/License:Expat def counter_factory(): count = 0 def counter(): nonlocal count count = count + 1 return count return counter partctr = counter_factory() chapctr = counter_factory() sectctr = counter_factory() print('Part {}, Part one'.format(str(partctr()))) print('Chapter {}, Chapter one'.format(str(chapctr()))) print('Section {}, Section one'.format(str(sectctr()))) print('Section {}, Section two'.format(str(sectctr()))) print('Section {}, Section three'.format(str(sectctr()))) sectctr = counter_factory() ## Restart section count print('Chapter {}, Chapter Two'.format(str(chapctr()))) print('Section {}, Section one'.format(str(sectctr()))) print('Part {}, Part two'.format(str(partctr()))) sectctr = counter_factory() ## Restart section count print('Chapter {}, Chapter Three'.format(str(chapctr()))) print('Section {}, Section one'.format(str(sectctr()))) sectctr = counter_factory() ## Restart section count print('Chapter {}, Chapter Four'.format(str(chapctr()))) print('Section {}, Section one'.format(str(sectctr()))) print('Section {}, Section one'.format(str(sectctr())))