tuple , characters , int.. which of immutable objects do not change.
so, immutable objects, there is no real difference between call by sharing and call by value
mutable object
list , set , dictionary , which can be changed
so, within the function are visible to the caller, which may appear to differ from call by value semantics.
because the object is not copied or cloned -> it is shared
# Python Call by Object reference
# Call by sharing
# immutable object
# For immutable objects, there is no real difference between call by sharing and call by value
a = 1
def immutable_object_test(num):
print( 'before allocated : {} '.format(id(num)) )
num = 10
print( 'after allocated : {} '.format(id(num)) )
print( 'a : {}'.format(id(a)))
immutable_object_test(a)
print( 'result : {}'.format(a))
print('-'*10)
# mutable object
# if the objects are mutable, within the function are visible to the caller, which may appear to differ from call by value semantics.
# Mutations of a mutable object within the function are visible to the caller because the object is not copied or cloned—it is shared.
a = [1,2,3,4]
def mutable_object_test( temp ):
print( 'before allocated : {} '.format(id(temp)) )
temp.append(5)
print( 'after allocated : {} '.format(id(temp)) )
print( 'a : {}'.format(id(a)))
mutable_object_test( a )
print( 'result : {}'.format(a))
def try_to_change_string_reference(the_string):
print('got', the_string)
the_string = 'In a kingdom by the sea'
print('set to', the_string)
outer_string = 'It was many and many a year ago'
print('before, outer_string =', outer_string)
try_to_change_string_reference(outer_string)
print('after, outer_string =', outer_string)
# 결과
before, outer_string = It was many and many a year ago
got It was many and many a year ago
set to In a kingdom by the sea
after, outer_string = It was many and many a year ago