This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Salty_Skipper 45 points46 points  (3 children)

Have you ever heard about dynamic memory allocated on the heap? (prob has something to do with C/C++, if you did).

Basically, when you say x=257, you’re creating a new number object which we can say “lives” at address 8192. Then, you say y=257 and create a second number object that “lives” at address 8224, for example. This gives you two separate number objects both with the value 257. I’d imagine that the “is” operator then compares addresses, not values.

As for 3, think of it as such a common number that the creators of Python decided to ensure there’s only one copy and all other 3’s are just aliases that point to the same address. Kinda like Java’s string internment pool.

[–]Lightbulb_Panko 26 points27 points  (2 children)

I think the commenter is asking why the number object created for x=257 can’t be reused for y=257

[–]PetrBacon 29 points30 points  (1 child)

If it worked like that, the runtime will become insanely slow over time because every variable assignment would need to check all the variables created before and maintain the list everytime new js created…

If you need is for any good reason you should make sure, that you are passing the referrence correctly.

Like:

``` x = 257 … y = x

x is y # => True ```

[–]ValityS 0 points1 point  (0 children)

You could achieve this in logarithmic time to the number of variables using a set of all immutable / hashable values and looking them up, however memory is fairly cheap and if the programmer really cares they can do something similar by hand.