3

I am relatively new to python and would like to settle a dispute I am having with someone. Suppose you were given an exam or quiz and the following question appeared:

Question 32: In the following python [IDLE] example:

>>> x=52
>>> x 
52
>>> id(x)
505404728

505404728 is:

  • a) the contents of x
  • b) the ID of x
  • c) the ID of 52
  • d) (b) and (c)
  • e) none of the above

Please provide an explanation. Thanks everyone!

0

3 Answers 3

4

A useful-to-know excerpt from the docs:

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.

So, to recap:

In [200]: x = 52

In [201]: id(x)
Out[201]: 4297372320

In [202]: id(52)
Out[202]: 4297372320

x points to 52. 52 is stored in memory somewhere. id returns a "number" that is guaranteed to be unique for an object during its lifetime.

In short, the answer is d, because x and 52 have the same reference. You can argue that it might be c though... depending on how you interpret the question.

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

Comments

4

id returns an integer that is guaranteed to be unique and constant for this object during its lifetime

>>> x = 52
>>> y = 52
>>> id(x)
12694616
>>> id(y)
12694616

Different python implementations (Cpython, Jython, etc) implement this differently - cpython for example returns the memory address of the object.

Update: Actually, Christian Dean's answer in the comments is more accurate: the answer is uhhhh .... 'c' ? Does the label x have an id, or does is it a pointer to the value 52?

7 Comments

"To answer your question, I'd need to know exactly which python implementation this was concerning" - Not really. No matter what implementation you choose, you can always say 505409528 is the object's ID. You don't have to get specific. Now how exactly that ID was generated is implementation-specific. BTW, nice answer. +1
The answer is basically d. Why id returns what it does is an implementation detail. As to what it returns... you can be sure it's the ID always. The actual memory location is hidden away from the user, you cannot work with memory directly in python.
@cᴏʟᴅsᴘᴇᴇᴅ Ehhhh.....I'd say c rather than b. I would pick b, but the wording of b seems a bit incorrect. x its self has no ID, the object is refers to does. If b instead said something like "The ID of the object x refers to", then I could choose it.
@ChristianDean Point there... I need to sit and rethink this. :p
@cᴏʟᴅsᴘᴇᴇᴅ This is one of those "Haddock's Eyes" issues that only ever come up in job interviews ;) en.wikipedia.org/wiki/Haddocks%27_Eyes
|
2

I'd say the correct answer is c. id(x) returns the ID of the object that x refers to, 52. So c is correct. Here's why I didn't choose the others:

  • a: Option a is blatantly incorrect. We don't even have to search for a definitive answer. We can clearly see that value of c is 52, not 505409528. Also, x doesn't "hold" anything. It is a label that refers to the object 52.
  • b: Option b although better, is still incorrect. The variable x its self does not have an ID. It's simply a label. 52 - the object it's label-ing or referring to - does have an ID.
  • d: I didn't choose this one for the reason I didn't choose b.
  • e: The option is also incorrect. As I said above, I believe the correct answer is c.

In the future, to be able to fully understand and answer questions such as the one you posted, you need to have a firm understand of what variables are in Python. The defacto resource is an article by Ned Batchelder titled Facts and myths about Python names and values.

2 Comments

No, I disagree. The best answer is b. The "id of x" makes more sense that the "ID of 52". Variables are names that we use to refer to objects. That is why it makes sense to say x = object(); y = x; x is y. Indeed, 52 is just an int literal. It makes as much sense to say "the ID of []"
@juanpa.arrivillaga Alright, I understand your point. Actually, after sleeping on it, I'm seriously debating b vs c again. I would like to choose b over c, but it's wording still seems incorrect, a bit like a trick question. Either way though, this question does seem pretty vague and rather poor. More detail should really be added.

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.