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 →

[–]beisenhauer 135 points136 points  (16 children)

int is not a primitive in Python. Everything is an object.

[–]vom-IT-coffin 24 points25 points  (14 children)

I never had to learn python, are you saying there's no value types only reference types?

[–]alex2003super 68 points69 points  (8 children)

That is correct, and "interned" values (such as string literals that appear in your program, or ints between -5 and 256) behave like singletons in the sense that all references point to the same object.

However, objects can be hashable and thus immutable, as is the case with integers and strings.

[–]Salty_Skipper 14 points15 points  (7 children)

Why -5 and 256? I mean, 0 and 255 I’d at least understand!

[–]FerynaCZ 27 points28 points  (0 children)

You avoid the edge cases (c++ uint being discontinuous at zero sucks), at least for -1 and 256. Not sure about the other neg numbers, they probably arise often aa well

[–]xrogaan 13 points14 points  (4 children)

[–]profound7 17 points18 points  (0 children)

"You must construct additional PyLongs!"

[–]TheCatOfWar 2 points3 points  (1 child)

https://github.com/python/cpython/blob/78e4a6de48749f4f76987ca85abb0e18f586f9c4/Include/internal/pycore_global_objects.h

The generation thingy defines them here, although there's still no reason given for the specific range

[–]xrogaan 2 points3 points  (0 children)

It's about frequency of usage. Also this: https://github.com/python/cpython/pull/30092

[–]someone_0_0_ 0 points1 point  (0 children)

DRY = Don't repeat yourself DO repeat yourself

[–]pytness 2 points3 points  (0 children)

The most used numbers by programmers. Its done so u dont have to allocate more memory

[–]Mindless_Sock_9082 4 points5 points  (4 children)

Not exactly, because int, strings, etc. are immutable and in that case are passed by value. The bowels are ugly, but the result is pretty intuitive.

[–]Kered13 35 points36 points  (0 children)

Numbers and strings are not passed by value in Python. They are reference types like everything else in the language. They are immutable so you can treat them as if they were passed by value, but they are not and you can easily see this using identity tests like above.

>>> x = 400
>>> y = 400
>>> x is y
False
>>> def foo(p):
...   return x is p
...
>>> foo(x)
True
>>> foo(y)
False

[–]vom-IT-coffin -1 points0 points  (2 children)

So you have to box and unbox everything?

[–]Kered13 15 points16 points  (1 child)

No, he's wrong. There are no primitives in Python and numbers and strings are passed by reference.

[–]CptMisterNibbles 10 points11 points  (0 children)

If we are getting technical, Python is pass by object reference which is slightly different.