3
pickle
라이브러리로 놀고 있었는데 때로는 다른 클래스 인스턴스가 같은 메모리 위치에 있다는 것을 알게되었습니다. 다른 클래스 인스턴스가 동일한 메모리 위치를 사용합니다.
아래의 예는 모두
말했다 동작을 전시하고 있습니다 :class DemoClass:
def __init__(self):
self.name = 'demoName'
#example 1
for i in range(3):
print (DemoClass())
#example 2
[print(DemoClass()) for i in range(3)]
#Output for both example 1 and example 2
#Note that the memory locations are identical in the output
<__main__.DemoClass object at 0x00CEE610>
<__main__.DemoClass object at 0x00CEE610>
<__main__.DemoClass object at 0x00CEE610>
이 나에게 꽤 놀라운이며,이을 발생하는 이유 어쩌면 당신이 설명 할 수있다.
예상대로 프로그램이 작동하는 방식은 다음과 같습니다. 첫 번째 2 예에서
demo = [DemoClass() for i in range(3)]
for i in demo:
print (i)
#Output
<__main__.DemoClass object at 0x01F7E630>
<__main__.DemoClass object at 0x01F7ED30>
<__main__.DemoClass object at 0x01F7E670>