python built-in integer object -


i read article python retains number objects better performance. example:

x = 3 y = 3 print(id(x)) print(id(y)) 

gives out same values, means x , y referencing same object. article suggested retained number objects approximately in range 1~100.

so tested following code getting exact range:

for in range(-1000,1000):     x = int(str(i))     y = int(str(i))     if str(id(x)) == str(id(y)):         print(i) 

and result quite weird: prints out -5~256.

i'm wondering how these 2 magic numbers came , why they're being used. also, these 2 values change in different environment? thanks!

256 power of 2 , small enough people using numbers range.

-5 less sure about, perhaps special values?

related: what's integer cache inside python?

also word of wisdom thread:

this implementation detail, don't ever rely on happening or not happening


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -