1

I found a very strange interview question, at least for me.

If we declare the following variables, how many objects were created?

a=300, b=300, c=5, d=5

Can someone please explain this? I am not sure to which segment in Python this is related to. Is it maybe to memory management in Python?

8
  • They're basically asking if a and b refer to the same object in memory (along with c and d) Commented Oct 24, 2019 at 9:50
  • 2
    Possible duplicate of "is" operator behaves unexpectedly with integers Commented Oct 24, 2019 at 9:53
  • Alternative duplicate target stackoverflow.com/questions/15171695/… Commented Nov 3, 2019 at 18:30
  • Other than as a trivial question, I can't imagine why this question would be asked in an interview. Commented Aug 27 at 19:27
  • 1
    @wjandrea: I assume the idea was that those four variables were being defined, with the commas being English list syntax between simple descriptions of what each assignment was, not Python code. But who knows? Commented Aug 28 at 19:57

2 Answers 2

10

There is no single answer

c and d have always been the same cached object in all implementations of CPython (you could technically tweak the size of the small int cache in a custom build, but all common builds would include 5). So we can say there are at most three unique objects here (technically, c = 5 and d = 5 don't even create objects; the small int cache created them during interpreter setup, before it even compiled this code, but 5 is at least a unique object). We also know there are at least two objects, because a 5 clearly isn't the same object as a 300. Outside CPython, we can't even limit the maximum to three; all four integers could be unique objects in an alternate interpreter.

But even if we stick to CPython, CPython's varying caching rules make the question about a and b unanswerable question without additional information. Specifically, in modern versions (unsure when it was introduced), all integer constants within a single module are deduplicated. In interactive mode though, this deduplication doesn't happen if they're on separate lines, and might not happen if they're on a single semicolon separated line.

So running a = 300; b = 300; print(a is b) in the interactive interpreter will print (sometimes, depends on exact interpreter configuration and maybe Python version) False. Running:

>>> a = 300
>>> b = 300
>>> print(a is b)

will reliably print False. But if you put the exact same code in a file (on one line or three), e.g. test_identity.py, then run python3 test_identity.py, it will reliably output True. On the other hand, if you wrote two modules:

# a.py
a = 300

and

# b.py
b = 300

and then ran import a, b; print(a.a is b.b), you'll get False again; int constant deduplication is limited to the module-level, it doesn't occur between modules like string literal interning does. Thus, depending how where the code is executed, there could be two or three unique objects involved. And even then, the Python the language makes no guarantees that either object is deduplicated, so the range is 2-3 for the CPython implementation, but 2-4 for Python the language.

As a side-note, none of these objects are created when the line is run (this gets a little weird, definition-wise, in the interactive interpreter where both compilation and execution are combined, but in a module, there is a clear distinction). They're created and cached when the line is compiled. If the same line in the same file is executed again (as part of a loop, or a repeatedly called function, or whatever) the same constant int value is loaded, so arguably, no objects were created: the 5s were created in the small int cache when Python launched before it even began running Python-level code, and the 300(s) were created when the Python-level code was compiled, and running the code merely loads them.

This is a terrible interview question

Really, the answer here is that this is either a poor interview question, or they're looking for people with deep knowledge of CPython internals. As the various other answers here demonstrate, even people who think they know the int cache rules miss other caching rules that change the answer to the question in different contexts.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried a = 300; b = 300; print(a is b) in the interactive interpreter and it printed True, as I had expected.
Fun. I actually expected it to do that, but at least on my system at work, on 3.13, in both plain Python REPL and IPython, it printed False. At home, on 3.12, it printed False in IPython, and True in plain Python REPL. It doesn't seem like something that would be version dependent, so it's possible I have something weird in my REPL config. It definitely prints False if you execute the lines sequentially, rather than stringing them together with semi-colons to execute as a single line.
Sometimes the purpose of an interview question isn't to get an answer, but to determine if you know how to ask the questions important to determining the answer. If you start making assumptions about e.g. whether you're using CPython, that can be a red flag.
Yeah, I get that argument, but when I've been given these things, they're usually higher level design questions that let you demonstrate anything from "This is a topic I know in-depth and can answer correctly on the spot" to "I don't know this, but I can show my thought process so you know I can solve new problems". Nitpicky stuff like this lets you see unwarranted assumptions, but it's such a tiny detail, one wholly irrelevant to 99.99% of all Python users (the correct answer is "99.999% of the time, it doesn't matter they're the same object"), with little scope to demonstrate much.
6

There are 3 different objects, from the Python docs (Integer objects):

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

To answer the question explicitly, there are 3 different objects, but only 2 get created, namely, a and b. (Thanks to @jpa for pointing this out!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.