Guest User

Untitled

a guest
Nov 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. # temp.py
  2. def str_to_global_var(string, obj):
  3. command_1 = 'global %s; %s=%r' % (string, string, obj)
  4. exec(command_1)
  5.  
  6.  
  7.  
  8. # script.py
  9. from copy import copy
  10. from temp import str_to_global_var
  11.  
  12. cur_global = copy(globals())
  13. str_to_global_var('x', 6)
  14.  
  15. new_global = globals()
  16.  
  17. print(set(new_global.keys()) - set(cur_global.keys()))
  18.  
  19. d2 = copy(new_global)
  20. d1 = copy(cur_global)
  21.  
  22. del d2['cur_global']
  23. del d2['new_global']
  24.  
  25. print('Compare d2==d1: ', d2 == d1)
  26. print("'x' in new_global: ",'x' in new_global)
  27.  
  28. import temp
  29. print("temp.x:", temp.x)
  30.  
  31.  
  32.  
  33. # Interpreter running result
  34. >>> ipython3 script.py
  35.  
  36. {'new_global', 'cur_global'}
  37. Compare d2==d1: True
  38. 'x' in new_global: False
  39. temp.x: 6
Add Comment
Please, Sign In to add comment