Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52324 > unrolled thread

Re: Python Basic Doubt

Started byDennis Lee Bieber <wlfraed@ix.netcom.com>
First post2013-08-10 16:30 -0400
Last post2013-08-11 08:29 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python Basic Doubt Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-10 16:30 -0400
    Re: Python Basic Doubt Roy Smith <roy@panix.com> - 2013-08-10 16:42 -0400
      Re: Python Basic Doubt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-11 08:29 +0000

#52324 — Re: Python Basic Doubt

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-08-10 16:30 -0400
SubjectRe: Python Basic Doubt
Message-ID<mailman.439.1376166663.1251.python-list@python.org>
On Sat, 10 Aug 2013 21:03:36 +0530, Krishnan Shankar
<i.am.songoku@gmail.com> declaimed the following:

>
>>>> a=10
>>>> id(a)
>21665504
>>>> b=a
>>>> id(b)
>21665504
>>>> c=10
>>>> id(c)
>21665504
>
>I am actually assigning new value to c. But from the value of id() all
>three variables take same location. With variables a and b it is ok. But
>why c taking the same location?
>

	Because id(n) is not giving you the address of the NAME. It is giving
you the address of the "10" -- and small integers are cached and reused in
most Python implementations.

	Compare:

>>> a = 12345
>>> id(a)
56674472L
>>> b = a
>>> id(b)
56674472L
>>> c = 12345
>>> id(c)
56674688L
>>> a = 11
>>> id(a)
4472248L
>>> b = a
>>> id(b)
4472248L
>>> c = 11
>>> id(c)
4472248L
>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [next] | [standalone]


#52326

FromRoy Smith <roy@panix.com>
Date2013-08-10 16:42 -0400
Message-ID<roy-E1ECF8.16422210082013@news.panix.com>
In reply to#52324
In article <mailman.439.1376166663.1251.python-list@python.org>,
 Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:

> Because id(n) is not giving you the address of the NAME. It is giving
> you the address of the "10"

Actually, it is giving you the id of the int(10) object.  Maybe it's an 
address, maybe it's not.  Only your implementation knows for sure.

[toc] | [prev] | [next] | [standalone]


#52369

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-11 08:29 +0000
Message-ID<52074b7a$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#52326
On Sat, 10 Aug 2013 16:42:22 -0400, Roy Smith wrote:

> In article <mailman.439.1376166663.1251.python-list@python.org>,
>  Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> 
>> Because id(n) is not giving you the address of the NAME. It is giving
>> you the address of the "10"
> 
> Actually, it is giving you the id of the int(10) object.  Maybe it's an
> address, maybe it's not.  Only your implementation knows for sure.

/steve cheers from the audience

Thank you for mentioning this. Using Jython:

>>> x = 10
>>> id(x)
1


And using IronPython:

>>> x = 10
>>> id(x)
43


"id" does not stand for "memory address". It stands for "identity".


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web