저는 파이썬 객체의 메모리 할당에 대해 혼란스러워합니다. 사전 정의 된 유형의 할당이 일관되게 작동하지 않는 것 같습니다. a
및 b
에 대한파이썬 : 메모리 관리 최적화 불일치?
a = None
b = None
print(a, b is a) # it outputs True, one single instance of None
a = 'a'
b = 'a'
print(a, b is a) # it outputs True, one single instance of a string
a = 2
b = 2
print(a, b is a) # it outputs True, one single instance of an int
a = 2.5
b = 2.5
print(a, b is a) # it outputs True, one single instance of a float
# from the python command line 'b is a' returns False
a = 'a b'
b = 'a b'
print(a, b is a) # it outputs True, one single instances of the same string
# from the python command line 'b is a' returns False
a =()
b =()
print(a, b is a) # it outputs True, one single instance of a()
a = {}
b = {}
print(a, b is a) # it outputs False, two different instances of the same empty {}
a = []
b = []
print(a, b is a) # it outputs False, two different instances of the same []
id
반환 값이 is
운영자가 제대로 작동하지만, '메모리 사용을 최적화'알고리즘이 일관성없이 작동하는 것 같군 보여 다음은 문제에 대한 내 cogitations의 제품입니다.
마지막 두 개의 print
출력과 파이썬 명령 줄 인터프리터 동작이 구현 버그를 드러내는가 아니면 Python이 그런 식으로 행동해야합니까?
OpenSUSE 13.1 env에서 테스트를 실행했습니다. 파이썬 2.7.6 및 파이썬 3.3.5 (기본, 3 월 27 일 2014, 17:16:46) [GCC] on linux.
명령 줄과 프로그램 간의 출력 차이를 제외하고 이러한 유형의 최적화가 필요한 이유는 무엇입니까? 저는 프로그래머가 직접 관리해야하는 특별한 경우를 고려하지 않는다면 평균 10 % 이상의 메모리를 절약 할 수 있다고 가정하는 것이 매우 낙관적 인 것이라고 생각합니다.
메모리 조각화를 효과적으로 최소화하려면이 동작이 도움이됩니까?
항목 : http://stackoverflow.com/ q/15541404/3001761, http://stackoverflow.com/q/21203212/3001761. – jonrsharpe