Purpose
It is used to collect sample code for python's coding tips of initialization variables.
A. Variables with constants
test_init_vars.py
#!/usr/bin/env python a=0 b=1 c=2 d=3 e=4 f=5 print(a, b, c, d, e, f) |
B. Use range()
#!/usr/bin/env python a, b, c, d, e, f = range(6) print(a, b, c, d, e, f) |
C. Use enum since python 3.4 supported
test_init_vars_by_enum.py
#!/usr/bin/env python3.6 from enum import IntEnum,auto class Vars(IntEnum): a=0 b=auto() c=auto() d=auto() e=auto() f=auto() print("{}, {}, {}, {}, {}, {}".format(Vars.a, Vars.b, Vars.c, Vars.d, Vars.e, Vars.f)) |
Result
$ python2.7 test_init_vars.py (0, 1, 2, 3, 4, 5) $ python3.6 test_init_vars.py 0 1 2 3 4 5 (0, 1, 2, 3, 4, 5) $ python3.6 test_init_vars_by_range.py 0 1 2 3 4 5 $ python2.7 test_init_vars_by_enum.py Traceback (most recent call last): File "test_init_vars_by_enum.py", line 3, in from enum import IntEnum,auto ImportError: cannot import name auto 0, 1, 2, 3, 4, 5 |