Assigning a value to a global variable inside a function [duplicate]
This question already has an answer here:
Don't understand why UnboundLocalError occurs [duplicate]
8 answers
a = 2
def alter_a():
a = a * 2
return a
Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?
Sssign a new value to a... which is the old value * 2. Why isn't this possible?
python python-3.x scope
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();
}
);
});
});
Nov 23 '18 at 9:17
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:
Don't understand why UnboundLocalError occurs [duplicate]
8 answers
a = 2
def alter_a():
a = a * 2
return a
Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?
Sssign a new value to a... which is the old value * 2. Why isn't this possible?
python python-3.x scope
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();
}
);
});
});
Nov 23 '18 at 9:17
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:
Don't understand why UnboundLocalError occurs [duplicate]
8 answers
a = 2
def alter_a():
a = a * 2
return a
Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?
Sssign a new value to a... which is the old value * 2. Why isn't this possible?
python python-3.x scope
This question already has an answer here:
Don't understand why UnboundLocalError occurs [duplicate]
8 answers
a = 2
def alter_a():
a = a * 2
return a
Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?
Sssign a new value to a... which is the old value * 2. Why isn't this possible?
This question already has an answer here:
Don't understand why UnboundLocalError occurs [duplicate]
8 answers
python python-3.x scope
python python-3.x scope
asked Nov 23 '18 at 9:15
BrowserMBrowserM
214
214
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();
}
);
});
});
Nov 23 '18 at 9:17
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();
}
);
});
});
Nov 23 '18 at 9:17
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 |
add a comment |
1 Answer
1
active
oldest
votes
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2
add a comment |
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2
add a comment |
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2
answered Nov 23 '18 at 9:17
NetwaveNetwave
12.5k22144
12.5k22144
add a comment |
add a comment |