How to remove item from locals()? [duplicate]












0
















This question already has an answer here:




  • Any way to modify locals dictionary?

    5 answers




I'm working on a script that requires manipulation of locals(), and I'm having trouble removing values from it. I've tried locals().pop(key) and del locals()[key], but neither works.



As an example:



def locals_test():
a, b = 1, 2
locals().pop('a')
del locals()['b']
return locals()

def dict_test():
test = {'a':1, 'b':2}
test.pop('a')
del test['b']
return test

print(locals_test()) # --> {'a':1, 'b':2}
print(dict_test()) # --> {}


I'm trying to replicate the behavior of dict_test() in locals_test(), but I've yet to find a solution. Does anyone know how to solve this?










share|improve this question















marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 23:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

    – juanpa.arrivillaga
    Jan 1 at 23:03








  • 2





    locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

    – Martijn Pieters
    Jan 1 at 23:09











  • I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

    – aedificatori
    Jan 1 at 23:13






  • 1





    "by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

    – juanpa.arrivillaga
    Jan 1 at 23:20











  • Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

    – aedificatori
    Jan 1 at 23:28
















0
















This question already has an answer here:




  • Any way to modify locals dictionary?

    5 answers




I'm working on a script that requires manipulation of locals(), and I'm having trouble removing values from it. I've tried locals().pop(key) and del locals()[key], but neither works.



As an example:



def locals_test():
a, b = 1, 2
locals().pop('a')
del locals()['b']
return locals()

def dict_test():
test = {'a':1, 'b':2}
test.pop('a')
del test['b']
return test

print(locals_test()) # --> {'a':1, 'b':2}
print(dict_test()) # --> {}


I'm trying to replicate the behavior of dict_test() in locals_test(), but I've yet to find a solution. Does anyone know how to solve this?










share|improve this question















marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 23:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

    – juanpa.arrivillaga
    Jan 1 at 23:03








  • 2





    locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

    – Martijn Pieters
    Jan 1 at 23:09











  • I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

    – aedificatori
    Jan 1 at 23:13






  • 1





    "by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

    – juanpa.arrivillaga
    Jan 1 at 23:20











  • Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

    – aedificatori
    Jan 1 at 23:28














0












0








0









This question already has an answer here:




  • Any way to modify locals dictionary?

    5 answers




I'm working on a script that requires manipulation of locals(), and I'm having trouble removing values from it. I've tried locals().pop(key) and del locals()[key], but neither works.



As an example:



def locals_test():
a, b = 1, 2
locals().pop('a')
del locals()['b']
return locals()

def dict_test():
test = {'a':1, 'b':2}
test.pop('a')
del test['b']
return test

print(locals_test()) # --> {'a':1, 'b':2}
print(dict_test()) # --> {}


I'm trying to replicate the behavior of dict_test() in locals_test(), but I've yet to find a solution. Does anyone know how to solve this?










share|improve this question

















This question already has an answer here:




  • Any way to modify locals dictionary?

    5 answers




I'm working on a script that requires manipulation of locals(), and I'm having trouble removing values from it. I've tried locals().pop(key) and del locals()[key], but neither works.



As an example:



def locals_test():
a, b = 1, 2
locals().pop('a')
del locals()['b']
return locals()

def dict_test():
test = {'a':1, 'b':2}
test.pop('a')
del test['b']
return test

print(locals_test()) # --> {'a':1, 'b':2}
print(dict_test()) # --> {}


I'm trying to replicate the behavior of dict_test() in locals_test(), but I've yet to find a solution. Does anyone know how to solve this?





This question already has an answer here:




  • Any way to modify locals dictionary?

    5 answers








python dictionary local-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 1:06









martineau

68.4k1090183




68.4k1090183










asked Jan 1 at 22:59









aedificatoriaedificatori

557




557




marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 23:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 23:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

    – juanpa.arrivillaga
    Jan 1 at 23:03








  • 2





    locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

    – Martijn Pieters
    Jan 1 at 23:09











  • I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

    – aedificatori
    Jan 1 at 23:13






  • 1





    "by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

    – juanpa.arrivillaga
    Jan 1 at 23:20











  • Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

    – aedificatori
    Jan 1 at 23:28














  • 3





    Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

    – juanpa.arrivillaga
    Jan 1 at 23:03








  • 2





    locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

    – Martijn Pieters
    Jan 1 at 23:09











  • I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

    – aedificatori
    Jan 1 at 23:13






  • 1





    "by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

    – juanpa.arrivillaga
    Jan 1 at 23:20











  • Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

    – aedificatori
    Jan 1 at 23:28








3




3





Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

– juanpa.arrivillaga
Jan 1 at 23:03







Changes to the dictionary returned by locals will not affect the local namespace. Did you read the documentation for locals? Why do you believe you even need to manipulate locals???

– juanpa.arrivillaga
Jan 1 at 23:03






2




2





locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

– Martijn Pieters
Jan 1 at 23:09





locals() is a one-way street. You can't remove local variables by deleting from the locals() dictionary.

– Martijn Pieters
Jan 1 at 23:09













I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

– aedificatori
Jan 1 at 23:13





I missed that part of the documentation, looks like. It didn't occur to me that the returned dictionary would be by reference as opposed to the actual objects. As for why I'm even doing this, I'm working on a Python interface written in Python, and I'm pulling locals between snippets of code.

– aedificatori
Jan 1 at 23:13




1




1





"by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

– juanpa.arrivillaga
Jan 1 at 23:20





"by reference as opposed to the actual objects" What? No, that isn't what's going on. locals simply builds a dict on each invocation from the local namespace. It returns that dict, but local namespaces aren't affected by modifying that dict. But that dict contains the actual objects being referenced in the local namespace. Anyway, that does seem like a good reason to use locals.

– juanpa.arrivillaga
Jan 1 at 23:20













Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

– aedificatori
Jan 1 at 23:28





Ahh, sorry, I used the wrong words for that. Build by invocation makes a lot of sense, though, thank you for clarifying. I appreciate it.

– aedificatori
Jan 1 at 23:28












1 Answer
1






active

oldest

votes


















2














locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:



def locals_test():
a, b = 1, 2
del a
del b
return locals()


General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.






share|improve this answer
























  • Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

    – aedificatori
    Jan 1 at 23:13




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:



def locals_test():
a, b = 1, 2
del a
del b
return locals()


General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.






share|improve this answer
























  • Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

    – aedificatori
    Jan 1 at 23:13


















2














locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:



def locals_test():
a, b = 1, 2
del a
del b
return locals()


General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.






share|improve this answer
























  • Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

    – aedificatori
    Jan 1 at 23:13
















2












2








2







locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:



def locals_test():
a, b = 1, 2
del a
del b
return locals()


General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.






share|improve this answer













locals() returns a dictionary which is a copy of the local variables. Changing a copy won't remove the variable. The way that works:



def locals_test():
a, b = 1, 2
del a
del b
return locals()


General advice: playing with locals & globals like this is often the symptom of a XY problem. You'd be better off with a dictionary of values so you're not polluted by the rest of the local variables.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 23:05









Jean-François FabreJean-François Fabre

105k955112




105k955112













  • Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

    – aedificatori
    Jan 1 at 23:13





















  • Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

    – aedificatori
    Jan 1 at 23:13



















Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

– aedificatori
Jan 1 at 23:13







Got it, thanks. I'd missed the part of the documentation where it described that the output is a copy of the local variables. (And goodness, do I wish it were an XY problem.)

– aedificatori
Jan 1 at 23:13







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'