-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter_py2.py
More file actions
executable file
·28 lines (22 loc) · 838 Bytes
/
counter_py2.py
File metadata and controls
executable file
·28 lines (22 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python2
'''Python 2 counter closure'''
from __future__ import print_function
def init_counter(count=0):
'''(re)set counter that increments whenever called (closure)'''
_count_dict = {'count' : count} # put the enclosed value in a dict
def _increment_counter(): # to avoid rebinding (for Python 2)
'''inner incrementer function'''
_count_dict['count'] += 1
return _count_dict['count']
return _increment_counter
def main():
'''test driver for init_counter'''
counter = init_counter(6)
print("counter() =>", counter())
print("counter() => %d" % counter())
print("Re-initializing counter...")
counter = init_counter(0)
print("counter() =>", counter())
print("counter() => %d" % counter())
if __name__ == '__main__':
main()