3 ms·
> We can use the Python built-in function id which returns a value you can think of as a memory address to investigate. > [...] > It looks like there is a tab
by boramalper 9y ago
> We can use the Python built-in function id which returns a value you can think of as a memory address to investigate.
> [...]
> It looks like there is a table of tiny integers and each integer is takes up 32 bytes.
It is the memory address but it's a "CPython implementation detail: This [return value of the id() function] is the address of the object in memory."[1]
Though you cannot use this to determine the size of an object, or rather you "shouldn't" because that assumes a very specific implementation detail, which isn't there.
If you'd like to get the size of an object, use sys.getsizeof().[2] Also keep in mind that containers in Python does not contain the objects themselves but references to them so the returned size is the size of the object itself only, non-recursively. Read "Is Python call-by-value or call-by-reference? Neither."[3] for some more details.
[1]: https://docs.python.org/3.6/library/functions.html#id https://docs.python.org/3.6/library/functions.html#id
[2]: http://docs.python.org/3.6/library/sys.html#sys.getsizeof http://docs.python.org/3.6/library/sys.html#sys.getsizeof
[3]: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-...
- int_19h 9y agoPython memory model is amazingly simple and consistent: everything is passed and stored by value, but all values are references to objects.
- Phrodo_00 9y agoI wouldn't say it's necessarily neither. While the concept of binding used in [3] is consistent, you can also simply say that functions are call-by-value and all values are references. Pretty much the same way C uses pointers when passed to a function. BTW, I didn't like the way the difference between mutable and immutable objects was pointed out, it sounded like the language recognized and supported those two types, when in fact the difference is just in the methods exposed by the objects. Even immutable objects can be modified if you don't respect the contracts (like in the case of this article).