This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author LambertDW
Recipients LambertDW, Orlowski, loewis
Date 2009-01-31.01:58:48
SpamBayes Score 0.0012891327
Marked as misclassified No
Message-id <84B204FFB016BA4984227335D8257FBA8C9490@CVCV0XI05.na.corning.com>
In-reply-to <4983849F.4070800@genesilico.pl>
Content
#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')
History
Date User Action Args
2009-01-31 01:58:50LambertDWsetrecipients: + LambertDW, loewis, Orlowski
2009-01-31 01:58:48LambertDWlinkissue5092 messages
2009-01-31 01:58:48LambertDWcreate