On Tue, Jul 23, 2013 at 12:09 AM, Jim Mooney <cybervigila...@gmail.com>wrote:
> I've noticed that when I create a number of objects from a class, one > after another, they're at different IDs, but the IDs all appear to be > equidistant, so that they all appear to have the same size. But what does > that size represent? is it bytes? ie - if I run this over and over I get > different id numbers but they are always 40 apart. Or is that distance > machine-specific as to units?: > > > 41599624 - different results for each run but always 40 apart > 41599664 > 41599704 > ''' > When I saved your code as a file on my machine (Python 2.7 on Win8 64-bit) and ran it from the command line, I got the same result as you - three IDs, 40 apart every time. However, I initially pasted your code into PyScripter, and when I ran it inside PyScripter the results were different: if the first ID was x, then the second was x+160, and the third was x+200. (160 is obviously divisible by 40... I have no idea what conclusion to draw from that.) I simplified the test-harness portion as follows: kronkList = [] for kronknum in xrange(0,100): kronkList.append(Kronk(kronknum)) print(kronkList[kronknum].getzar()) print(kronkList[kronknum].getself()) and now the ID is incremented by 40 each time regardless how I run the script. Each time I run it, the last digit stays the same throughout the run (as you would expect, given that we're adding 40 each time), but the last digits are NOT consistent _between_ runs. It appears to always be an even number, though. As an experiment, I added a couple of extra methods and attributes to the Kronk class, but it didn't change anything - IDs still incremented by 40 each time. eryksun will no doubt chime in to tell us exactly how object IDs are derived in cPython, but I'll go out on a limb and say that they have nothing to do with the size of the object; also that one would be very silly to make a program rely in any way on predicting the next ID, since external factors may throw off the calculations (as when I invoked your original script from inside PyScripter). All that you can rely on is that each unique object (i.e. doesn't pass an "is" comparison with any other object) has a unique ID... and, for all I know, not even that.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor