Advertisement
here2share

# variable_tracker.py

Aug 12th, 2022
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. # variable_tracker.py
  2.  
  3. xxx = 'xxx'
  4. yyy = 'yyy'
  5. ddd = {xxx: 5, yyy: 0}
  6.  
  7. def test():
  8.     print(tr(xxx))
  9.     print(tr(yyy))
  10.     print(tr(yyy))
  11.     print(tr(yyy))
  12.     print(tr(yyy,0))
  13.     print(tr(yyy))
  14.     print(tr(yyy))
  15.     print(tr(xxx))
  16.     print(tr('zzz',99))
  17.     print(tr(zzz))
  18.  
  19. def tr(var, i='void'):
  20.     if var in ddd:
  21.         if i != 'void':
  22.             ddd[var] = i
  23.         else:
  24.             ddd[var] += 1
  25.         return ddd[var]
  26.     else:
  27.         exec('global '+var+'; '+var+' = "'+var+'"') # caution: exec() can corrupt if used unwisely !!!
  28.         ddd[var] = i
  29.         return i
  30.  
  31. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement