-
[list] reference and copy (aliasing and clone)🐍 파이썬/파이썬 기본 문법 2022. 7. 19. 06:42
Reference : multiple variables can point same object
x = [1,2,3,4,5] y = x # this is called aliasing print(y[1]) # 2 y[1] = 0 print(y) # [1,0,3,4,5] print(x) # [1,0,3,4,5]
copy
a = [1,2,3] b = a[:] # copy or clone print(b) # [1,2,3] a[0] = 10 print(a) # [10,2,3] print(b) # [1,2,3] modifing a does not affect b
Equality and Identity Operators
== checks, if two objects are equal
is checks, if two objects are the exact same
a = [1,2] b = a # aliasing c = a[:] # cloning a == b # True a == c # True a is b # True a is c # False
'🐍 파이썬 > 파이썬 기본 문법' 카테고리의 다른 글
[class] python-specific special method (0) 2022.07.27 [OOP] class 클래스 개념 및 구성요소 (0) 2022.07.27 [List] 자주쓰이는 리스트 메소드 정리 (0) 2022.07.19 [concept] list vs tuple (0) 2022.07.19 [Variable scope] global scope vs function scope (0) 2022.07.18