ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] - Call by Object reference & Call by sharing
    개발/Python 2022. 7. 28. 15:08

    1. Call by value


    • call by value used to C programming
    • call the function , parameter copied value and pass value arguement
    • so, do not changed origin data within function

    2. Call by reference (pointer)


    • call by reference used to Java programming
    • parameter copied reference = pointer (value address)
    • so, function can access origin data by address

    3. Call by object reference( Call by sharing )


    Python's Object devided into 2 objects ( mutable object & immutable object )

     

    immutable object

    • 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_list_contents(the_list):
        print('got', the_list)
        the_list.append('four')
        print('changed to', the_list)
    
    outer_list = ['one', 'two', 'three']
    
    print('before, outer_list =', outer_list)
    try_to_change_list_contents(outer_list)
    print('after, outer_list =', outer_list)
    
    # 결과
    before, outer_list = ['one', 'two', 'three']
    got ['one', 'two', 'three']
    changed to ['one', 'two', 'three', 'four']
    after, outer_list = ['one', 'two', 'three', 'four']
    def try_to_change_list_reference(the_list):
        print('got', the_list)
        the_list = ['and', 'we', 'can', 'not', 'lie']
        print('set to', the_list)
    
    outer_list = ['we', 'like', 'proper', 'English']
    
    print('before, outer_list =', outer_list)
    try_to_change_list_reference(outer_list)
    print('after, outer_list =', outer_list)
    
    # 결과
    before, outer_list = ['we', 'like', 'proper', 'English']
    got ['we', 'like', 'proper', 'English']
    set to ['and', 'we', 'can', 'not', 'lie']
    after, outer_list = ['we', 'like', 'proper', 'English']
    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

    '개발 > Python' 카테고리의 다른 글

    [Python] - First class functions  (0) 2022.07.28
    [Python] - Generator  (0) 2022.07.28
    [Python] - Counter  (0) 2022.07.27
    [Python] - Permutation & Combination  (0) 2022.07.27
    [Python] - N진수 표현 및 Ascii  (0) 2022.07.27

    댓글

Designed by Tistory.