본문 바로가기
언어 정리/python 개념이해,문법정리

python 값복사, 참조복사

by 알 수 없는 사용자 2024. 4. 1.

 

 

 


리스트, 딕셔너리, Queue 의 경우 참조복사 [ 참조복사를 피하고 싶으면 copy lib 이용 필요 ]

bool 타입, int, str 은 값복사

import copy
import queue
class AA():
    def aa(self):
        q = False
        w = 1
        e = 'a'
        r = {1: 2}
        t = [1, 2]
        y = queue.Queue()
        y.put(1)
        u = {1: 2}
        self.bb(q, w, e, r, t, y, u)
        print(q)  # 출력값 : False
        print(w)  # 출력값 : 1
        print(e)  # 출력값 : a
        print(r)  # 출력값 : {1: 2, 3: 4}
        print("--------------------------------------") # --------------------------------------
        print(t)  # 출력값 : [1, 2, 3, 4]
        while True:
            if y.empty():
                break
            else:
                print(f" y 의 Q : {y.get()}")    # 출력값 : y 의 Q : 1
                                                # 출력값 :  y 의 Q : 2
        print(u)  # 출력값 : {1: 2}
    def bb(self, q, w, e, r, t, y, u):
        q = True
        w = 3
        e = 'b'
        r[3] = 4
        t.append(3)
        t.append(4)
        y_tmp = y       # 참조복사
        y_tmp.put(2)
        u_tmp = copy.copy(u)
        u_tmp[3] = 4

 

출력값

댓글