1

i do the following:

a=12345

I am trying to undertstand the meaning of this.Please answer below questions.

  1. a points to the memory address of 12345 (True/False)

  2. If i do b=12345. Then b also points to the memoery address of 12345 (True/False)

  3. I have read that the ref count of 12345 should increase by 1 after b points to it. (True/False)

  4. How can i retrieve the memory address of 12345. I want to check that a and b both point to address of 12345.Please clarify

I tried using id function(it only show same memory location for <=255 range)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
fsociety
  • 977
  • 3
  • 12
  • 23
  • 1
    python creates the variable with name a and with value 12345. – Montreal Sep 07 '16 at 05:22
  • Sir,i already know what you said.My questions still remain unanswered – fsociety Sep 07 '16 at 05:23
  • Please read the block quote at the end of this post. http://stackoverflow.com/a/6101453/2308683 – OneCricketeer Sep 07 '16 at 05:23
  • 1
    Possible duplicate of ["is" operator behaves unexpectedly with integers](http://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – OneCricketeer Sep 07 '16 at 05:29
  • 2
    You're quite interested in memory addresses and pointers. Pyhon doesn't deal with those. Additionally different implementations of Python might (and will!) handle those things in a different way. – Matthias Sep 07 '16 at 05:32

1 Answers1

1
  1. "a points to the memory address of 12345 (True/False)"

    True.

  2. "If i do b=12345. Then b also points to the memoery address of 12345 (True/False)"

    Maybe. If you had assigned b=a, the b would point to the same memory location as a. With b=12345, the answer is unknown: there may be more than one copy of 12345 in memory.

  3. "I have read that the ref count of 12345 should increase by 1 after b points to it. (True/False)"

    True if a and b pointed to the same location: see above.

  4. "How can i retrieve the memory address of 12345. I want to check that a and b both point to address of 12345. Please clarify."

    To check if a and b point to the same memory location, use is as in a is b. For example, in the following, a and b point to different memory locations:

    >>> a = 12345
    >>> b = 12345
    >>> a is b
    False
    

    In the following, by contrast, they point to the same location:

    >>> a = 1
    >>> b = 1
    >>> a is b
    True
    
John1024
  • 109,961
  • 14
  • 137
  • 171
  • thanks for clear cut explanation..regarding your comment "the answer is unknown: there may be more than one copy of 12345 in memory.". Can we actually have more than one copy of the same integer in memory? – fsociety Sep 07 '16 at 05:34
  • 1
    @fsociety Yes. The results included above, for example, show `a is b` returns False for the case of `12345`. – John1024 Sep 07 '16 at 05:37
  • @John1024 why is `a is b == True` when `a = 1` and `b = 1` and not with 12345? – lang2 Sep 07 '16 at 05:56
  • @lang2 Read this for an explanation: http://stackoverflow.com/questions/6101379/what-happens-behind-the-scenes-when-python-adds-small-ints/6101453#6101453 . Beware, this is only true for the current reference implementation and not guaranteed. – Matthias Sep 07 '16 at 06:00
  • 1
    @Matthias If I run the same code non-interactivelly, it's `True` for 12345 also..... confused. – lang2 Sep 07 '16 at 06:18
  • @lang2 As discussed in Matthias' link, for efficiency, the interpreter seems to keep a pool of common integers. Depending on the vagaries of the optimization scheme, `a` and `b` might point to the same memory or they might not. As far as running python code goes, however, it makes no difference whether they occupy the same memory. The memory location is an artifact of the implementation. The code will run the same regardless. – John1024 Sep 07 '16 at 06:36
  • @fsociety Interesting. In my tests in an interactive interpreter, it can change during the course of the run. – John1024 Sep 07 '16 at 06:45
  • @John1024 Sir,please ignore the previous comment.The actual output is as below for interactive mode: >>> a=3 >>> b=2000 >>> id(a*b) 38390872L >>> id(6000) 38390872L >>> id(a*b) 38390896L >>> id(a*b) 38390872L >>> id(a*b) 38390896L >>> id(a*b) is id(6000) False – fsociety Sep 07 '16 at 06:49
  • 1
    `a` is just a name. It does not point to a memory address at all. See [Execution model / Naming and binding](https://docs.python.org/2/reference/executionmodel.html#naming-and-binding). `id(obj)` returning the address of the object is just an implementation detail of CPython and has nothing to do with name binding. – Ilja Everilä Sep 07 '16 at 13:03
  • @IljaEverilä That "Execution Model" doc says "Names refer to objects." I said names point to objects. Unless one brings baggage from another language, _refer_ and _point_, in this context, sound like synonyms to me. What do you see as the difference? – John1024 Sep 27 '16 at 06:53
  • There are many ways to refer to an object using a name (in CPython), for example as a [key to a `dict`](https://github.com/python/cpython/blob/master/Python/ceval.c#L2341), etc. My point was that "`a` points to the memory address of ..." to which you answered **yes** is not so simple in Python. One could argue that ultimately the name will resolve to an address, but usually "points to address of" is understood in the C sense and implying that Python works that way is [misleading](http://stackoverflow.com/questions/12423614/local-variables-in-python-nested-functions) at best. – Ilja Everilä Sep 27 '16 at 07:29
  • Additionally note that the words "memory" and "address" are not mentioned in the documentation. As @Matthias aptly put it, ["Pyhon doesn't deal with those"](http://stackoverflow.com/questions/39361706/meaning-of-assignment-operator-in-python2-7/39361837?noredirect=1#comment66052831_39361706). – Ilja Everilä Sep 27 '16 at 07:38