Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # temp.py
- def str_to_global_var(string, obj):
- command_1 = 'global %s; %s=%r' % (string, string, obj)
- exec(command_1)
- # script.py
- from copy import copy
- from temp import str_to_global_var
- cur_global = copy(globals())
- str_to_global_var('x', 6)
- new_global = globals()
- print(set(new_global.keys()) - set(cur_global.keys()))
- d2 = copy(new_global)
- d1 = copy(cur_global)
- del d2['cur_global']
- del d2['new_global']
- print('Compare d2==d1: ', d2 == d1)
- print("'x' in new_global: ",'x' in new_global)
- import temp
- print("temp.x:", temp.x)
- # Interpreter running result
- >>> ipython3 script.py
- {'new_global', 'cur_global'}
- Compare d2==d1: True
- 'x' in new_global: False
- temp.x: 6
Add Comment
Please, Sign In to add comment