Python에서 object(객체)란?
type, id, refcount, value 를 속성으로 가지고 있는 a chunk of data.
https://dsaint31.tistory.com/517
[Python] Variable (and Object)
Variable (and Object)1. 정의Python에서 Variable은 Memory에 할당된 Object를 참조하는 Name (=Reference, Identifier)에 불과하다.이 문서에서 Object는 Python에서의 Object로 type과 value, ID (CPython에서는 할당된 memory addr
dsaint31.tistory.com

1. Data Type (or Class)
type(obj)
# obj.__class__ # dunder 인 class 로 확인.
2. ID (CPython의 경우 주소에 해당)
id(obj)
- hex()를 통해 hexadecimal로 표시하여 memory address를 나타내는 경우가 일반적
- 아니면 f-string에서 x 형식문자코드를 이용.
3. 메모리에서 차지하고 있는 Size (bytes)
import sys
sys.getsizeof(obj)
- entry로 가지고 있는 객체들의 실제 크기는 제외됨.
- list 등의 container 객체에 대한 실제 크기는 요소들에 대한 iteration 으로 구해내야 함(코드로 구현 필요.)
4. Reference Count
sys.getrefcount(obj) # getrefcount에 의해 임시로 reference가 추가되므로 -1을 해줘야 원래 값.
- PVM에서 caching되고 있는 255이하의 정수들에선 32bit unsigned int의 최대값인 4294967295 로 나옴.
- 이는 해당 객체가 영구적으로 유지됨을 의미함.
5. Attributes
dir(obj) # 객체의 모든 속성과 메서드를 확인.
# obj.__dict__ # 객체 자신의 속성들 확인. (상속받은 것들은 나오지 않으며 built-in type에선 제공하지 않음.)
6. referrers 의 리스트
import gc
gc.get_referrers(obj) # 참조하고 있는 객체들의 리스트를 반환.
# 수치로 얻으려면 len(gc.get_referrers(obj)) 로 처리해야 함.
7. Super-Classes
obj.__class__.__bases__ # 직접적인 부모클래스.
obj.mro() # Method Resolution Order를 따라 모든 super-class반환.
'Python' 카테고리의 다른 글
[PyTorch] autograd 심화: grad_fn 과 custom operation 만들기 (0) | 2025.03.28 |
---|---|
[Py] Bitwise Operator (0) | 2025.03.26 |
[Ex] Numeric Datatype 다음의 연산들의 결과를 구해보고 그 이유를 설명해보라. (0) | 2025.03.17 |
[PyTorch] dtype 단축메서드로 바꾸기 (0) | 2025.03.14 |
[PyTorch] in-place 연산이란? (0) | 2025.03.13 |