a = 1 deffunc(): a += 1 print(a) func() # UnboundLocalError: local variable 'a' referenced before assignment
import random deffunc(ok): if ok: a = random.random() else: import random a = random.randint(1,10) return a func(True) # UnboundLocalError: local variable 'random' referenced before assignment
### Solve ### import random deffunc(ok): if ok: global random a = random.random() else: import random a = random.randint(1,10) return a func(True)
import time defreport(when = time.time()): return when print(report()) print(report()) # 1521033649.5745206 # 1521033649.5745206
### Solve ### defreport(when = None): if when isNone: when = time.time() return when print(report()) print(report()) # 1521033793.564442 # 1521033812.377632