On 05/07/2015 03: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?
def testid(K=10):
K += 10
return 'the ID is', id(K), K
*** Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600
32 bit (Intel)] on win32. ***
testid()
('the ID is', 505991936, 20)
testid()
('the ID is', 505991936, 20)
testid()
('the ID is', 505991936, 20)
K is certainly created new each time, but it is bound to the same
object, the one created when the function is defined.
K does not have an id(), that object does.
Since the object in this case is immutable, the += creates a new object
and binds that to K. In your particular code, you never call the id()
for the initialization object. I'd add another print, that shows the
id(K) before the +=
All of this is confused by the fact that small ints happen to be
interned by your paricular implementation. So you're getting the same
<int=20> object each time.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor