How to remove item from locals()? [duplicate]
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?
python dictionary local-variables
marked as duplicate by juanpa.arrivillaga
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.
add a comment |
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?
python dictionary local-variables
marked as duplicate by juanpa.arrivillaga
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 bylocalswill not affect the local namespace. Did you read the documentation forlocals? Why do you believe you even need to manipulatelocals???
– juanpa.arrivillaga
Jan 1 at 23:03
2
locals()is a one-way street. You can't remove local variables by deleting from thelocals()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.localssimply builds a dict on each invocation from the local namespace. It returns thatdict, 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 uselocals.
– 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
add a comment |
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?
python dictionary local-variables
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
python dictionary local-variables
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
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
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 bylocalswill not affect the local namespace. Did you read the documentation forlocals? Why do you believe you even need to manipulatelocals???
– juanpa.arrivillaga
Jan 1 at 23:03
2
locals()is a one-way street. You can't remove local variables by deleting from thelocals()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.localssimply builds a dict on each invocation from the local namespace. It returns thatdict, 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 uselocals.
– 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
add a comment |
3
Changes to the dictionary returned bylocalswill not affect the local namespace. Did you read the documentation forlocals? Why do you believe you even need to manipulatelocals???
– juanpa.arrivillaga
Jan 1 at 23:03
2
locals()is a one-way street. You can't remove local variables by deleting from thelocals()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.localssimply builds a dict on each invocation from the local namespace. It returns thatdict, 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 uselocals.
– 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
3
Changes to the dictionary returned by
localswill not affect the local namespace. Did you read the documentation forlocals? Why do you believe you even need to manipulatelocals???– juanpa.arrivillaga
Jan 1 at 23:03
2
locals()is a one-way street. You can't remove local variables by deleting from thelocals()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.
localssimply builds a dict on each invocation from the local namespace. It returns thatdict, 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 uselocals.– 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