[Tutor] pointer puzzlement

Emile van Sebille emile at fenx.com
Thu May 7 22:03:56 CEST 2015


On 5/7/2015 12:15 PM, Jim Mooney Py3.4.3winXP wrote:
> I find this a bit confusing. Since the ID of K remains the same, so it's
> the same object, why isn't it increasing each time. i.e, 20, 30, 40,. I
> understand that it's immutable but doesn't that mean K is created each time
> in local scope so it should have a different ID each time?

You're looking at the ID of an interned string:

 >>> testid()
('the ID is', 7515584L, 20)
 >>> testid()
('the ID is', 7515584L, 20)
 >>> testid()
('the ID is', 7515584L, 20)
 >>> id(20)
7515584L
 >>> id(10+10)
7515584L
 >>> id(19+1)
7515584L


Compare to:

def testid(K=1000000):
      K += 10
      return 'the ID is', id(K), K


Emile




More information about the Tutor mailing list