Look at the code first:
1
2
3
4
|
a
=
1
def
fun
(
a
)
:
a
=
2
print
a
#1
|
################
1
2
3
4
|
b
=
[
]
def
fun
(
b
)
:
b
.
append
(
1
)
print
b
#1
|
Which one of the above two output should be?
The first thing that needs to be clarified here is mutable objects and immutable objects. In python, strings, tuples, and numbers are unchangeable objects, while lists, dicts, etc. are objects that can be modified. When a reference is passed to a function, the function automatically copies a reference, so the first immutable object's reference to the value of a has no half-line relationship with the outside a=1, and the following application is a reference to the object itself, then The output value is naturally 1. Have you got this knowledge point?
Original http://group.jobbole.com/10960/?utm_source=blog.jobbole.com&utm_medium=sidebar-group-topic