David W. Lambert <lamber...@corning.com> added the comment:

#Ah!  Not a problem.  You need globals() and locals() dictionaries.
# as a python3 script, this message produces next couple lines output.

#method one
#yup, global a is gone
#method two
#{'gv': 'local here', 'name': 'gv'}
#yup, global gv is gone

print('method one')

a = ''

def Delete_a_global_variable():
    global a
    del a

Delete_a_global_variable()

try:
    a
    print('whoops!  this can never happen')
except NameError:
    print('yup, global a is gone')

print('method two')

gv = ''                                 # global variable

def delete_chosen_variable(name):
    del globals()[name]
    gv = 'local here'
    print(locals())

delete_chosen_variable('gv')

try:
    gv
    print('whoops!  this can never happen')
except NameError:
    print('yup, global gv is gone')

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5092>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to