[Tutor] Why is an instance smaller than the sum of its components?

Jugurtha Hadjar jugurtha.hadjar at gmail.com
Wed Feb 4 00:42:11 CET 2015


On 02/03/2015 11:40 PM, Peter Otten wrote:

> CPython already does this for many common values, e. g. small integers and
> variable names
>
>
>>>> a = 42
>>>> b = 42
>>>> a is b
> True
>>>> a = 300
>>>> b = 300
>>>> a is b
> False
>

The threshold seems to be 256 (last value where it evaluates to True):

 >>> a = 1
 >>> b = 1
 >>> same = True
 >>> while same:
...	a += 1
...	b += 1
...	same = a is b
...
 >>> a
257
 >>> b
257

Interesting that it does that, and interesting that it doesn't work for 
floats.


> To know its class the foo instance only needs a reference to the class
> object, not a complete copy of the class. This is typically provided by
> putting a pointer into the instance, and this consumes only 8 bytes on 64-
> bit systems.

Okay, I thought the way it was done was that each instance was a full, 
independent, citizen/entity; with everything copied as many times as 
there are instances, which somehow bothered me memory wise, but had an 
appeal of having them completely separated.

>...

Rest of the post is partially grasped and requires further reading to 
fully appreciate. Thank you very much for taking the time and for the 
example code.



-- 
~Jugurtha Hadjar,


More information about the Tutor mailing list