Is list pass by value or by reference? [duplicate]
This question already has an answer here:
How do I pass a variable by reference?
24 answers
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference:
lst = [1, 2]
def f(lst):
# lst = lst + [3] # seems pass by value
# lst += [3] # strange! same as above but seems pass by reference
lst = lst.append(3) # seems pass by reference
return lst
f(lst)
print(lst)
can anyone tell me what is going on?
python list
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 24 '18 at 16:06
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.
|
show 11 more comments
This question already has an answer here:
How do I pass a variable by reference?
24 answers
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference:
lst = [1, 2]
def f(lst):
# lst = lst + [3] # seems pass by value
# lst += [3] # strange! same as above but seems pass by reference
lst = lst.append(3) # seems pass by reference
return lst
f(lst)
print(lst)
can anyone tell me what is going on?
python list
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 24 '18 at 16:06
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.
2
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
1
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
I think you have all the links by now. I just suggest the print ofid(name)
in several places when you have doubts like that.
– progmatico
Nov 24 '18 at 15:17
1
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
1
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18
|
show 11 more comments
This question already has an answer here:
How do I pass a variable by reference?
24 answers
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference:
lst = [1, 2]
def f(lst):
# lst = lst + [3] # seems pass by value
# lst += [3] # strange! same as above but seems pass by reference
lst = lst.append(3) # seems pass by reference
return lst
f(lst)
print(lst)
can anyone tell me what is going on?
python list
This question already has an answer here:
How do I pass a variable by reference?
24 answers
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference:
lst = [1, 2]
def f(lst):
# lst = lst + [3] # seems pass by value
# lst += [3] # strange! same as above but seems pass by reference
lst = lst.append(3) # seems pass by reference
return lst
f(lst)
print(lst)
can anyone tell me what is going on?
This question already has an answer here:
How do I pass a variable by reference?
24 answers
python list
python list
asked Nov 24 '18 at 14:49
buzhidaobuzhidao
776626
776626
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 24 '18 at 16:06
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 24 '18 at 16:06
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.
2
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
1
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
I think you have all the links by now. I just suggest the print ofid(name)
in several places when you have doubts like that.
– progmatico
Nov 24 '18 at 15:17
1
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
1
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18
|
show 11 more comments
2
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
1
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
I think you have all the links by now. I just suggest the print ofid(name)
in several places when you have doubts like that.
– progmatico
Nov 24 '18 at 15:17
1
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
1
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18
2
2
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
1
1
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
I think you have all the links by now. I just suggest the print of
id(name)
in several places when you have doubts like that.– progmatico
Nov 24 '18 at 15:17
I think you have all the links by now. I just suggest the print of
id(name)
in several places when you have doubts like that.– progmatico
Nov 24 '18 at 15:17
1
1
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
1
1
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18
|
show 11 more comments
1 Answer
1
active
oldest
votes
It’s passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function.
It’s essentially the same as passing a pointer in C, or a reference type in Java.
The result of the +=
case is because that operator actually modifies the list in place, so the effect is visible outside the function. lst.append()
is also an in-place operation, which explains your last case.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It’s passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function.
It’s essentially the same as passing a pointer in C, or a reference type in Java.
The result of the +=
case is because that operator actually modifies the list in place, so the effect is visible outside the function. lst.append()
is also an in-place operation, which explains your last case.
add a comment |
It’s passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function.
It’s essentially the same as passing a pointer in C, or a reference type in Java.
The result of the +=
case is because that operator actually modifies the list in place, so the effect is visible outside the function. lst.append()
is also an in-place operation, which explains your last case.
add a comment |
It’s passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function.
It’s essentially the same as passing a pointer in C, or a reference type in Java.
The result of the +=
case is because that operator actually modifies the list in place, so the effect is visible outside the function. lst.append()
is also an in-place operation, which explains your last case.
It’s passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function.
It’s essentially the same as passing a pointer in C, or a reference type in Java.
The result of the +=
case is because that operator actually modifies the list in place, so the effect is visible outside the function. lst.append()
is also an in-place operation, which explains your last case.
edited Nov 24 '18 at 14:59
answered Nov 24 '18 at 14:54
arshajiiarshajii
102k18184251
102k18184251
add a comment |
add a comment |
2
The difference is in assignment, not passing semantics.
– MisterMiyagi
Nov 24 '18 at 14:57
1
See stackoverflow.com/questions/575196
– FMc
Nov 24 '18 at 14:57
I think you have all the links by now. I just suggest the print of
id(name)
in several places when you have doubts like that.– progmatico
Nov 24 '18 at 15:17
1
@martineau that is not true. The evaluation strategy is exactly the same for any type of object.
– juanpa.arrivillaga
Nov 24 '18 at 16:05
1
I am also sure @martineau knows it better than his own words...
– progmatico
Nov 24 '18 at 17:18