0

For example

a = 12345  # id(a) = 140127713009936
a = 6789  # id(a) = 140127713010448
b = 12345  # id(b) = 140127713009936

How is it possible that Python remembers somehow that integer object with value 12345 was created before and gives reference to it instead of creating new object? I understand how it works for values -5 to 256, but why it works even work greater numbers (and also for strings, though I don't know if it's the same mechanism)?

  • 1
    Depends on details which aren't currently included in the question. Are these in a Python module? Then the parser recognizes them as constants, same as a C compiler does when building a constant table. – Charles Duffy Jul 04 '20 at 14:09
  • 1
    That said, questions that are just idle curiosity are often off-topic here. Do you have a specific practical problem caused by this behavior? See https://meta.stackexchange.com/questions/170394/what-is-the-rationale-for-closing-why-questions-on-a-language-design for discussion of the relevant rule. – Charles Duffy Jul 04 '20 at 14:10
  • (The above said, you may find [What's with the integer cache maintained by the interpreter?](https://stackoverflow.com/questions/15171695/whats-with-the-integer-cache-maintained-by-the-interpreter) informative). – Charles Duffy Jul 04 '20 at 14:13
  • They aren't in separate module, they're in 'main' file. I'm starting to learn Python and I'm trying to understand it better. I think that it's important for programmer to understand what happens behind the curtains, is it costly etc. Maybe this isn't practical problem but lack of understanding causes them. – abc2343 Jul 04 '20 at 14:19
  • If you're running `python main.py`, the interpreter loads the file all at once, and interns _all_ the literals (of immutable datatypes) therein; that's really what I was trying to get at. That's distinct from running Python at a REPL or other interactive interpreter and entering the commands one-at-a-time. – Charles Duffy Jul 04 '20 at 15:09
  • (btw, [python string interning](https://stackoverflow.com/questions/15541404/python-string-interning) is another relevant preexisting Q&A entry). – Charles Duffy Jul 04 '20 at 15:12
  • That said, please try to distinguish between understanding the _language_ and understanding the _implementation_. Implementation details can change between interpreter versions even if the language stays the same; they can also be different between CPython, Jython, IronPython, etc.; so depending on implementation details makes your code fragile. It's often better **not** to learn them (except to the extent that they can help in performance optimizations, but even that can have pitfalls), but to focus on learning the language itself. – Charles Duffy Jul 04 '20 at 15:14
  • ...a well-designed programming language will define, _as part of the language spec_, guaranteed performance parameters for certain calls (f/e, "map lookups by key shall always be able to complete in amortized constant time"). Better to learn and trust the behaviors that are guaranteed to stay the same between versions, not the ones that just happen to be true right now. – Charles Duffy Jul 04 '20 at 15:16

1 Answers1

0

In python (for optimization reasons), everything is a pointer if you have two objects with the same value, therefore, python will put a pointer for each of the objects to the same location that contains that value.

see: https://realpython.com/pointers-in-python/

How python knows if the object were early created: python map the name of each object to a memory address that the value exists there.

If the value doesn't already exist python creates a new one.

Hope you find it's helpful!

yagil
  • 55
  • 1
  • 7