[Tutor] pointer puzzlement
Jim Mooney Py3.4.3winXP
cybervigilante at gmail.com
Thu May 7 22:54:10 CEST 2015
On 7 May 2015 at 13:03, Emile van Sebille <emile at fenx.com> wrote:
> Compare to:
>
> def testid(K=1000000):
> K += 10
> return 'the ID is', id(K), K
>
Ah, thanks. I forgot small integers are saved in a table. I was looking at
a demo that pointers to defaults in function parameters are persistent. It
used lists so I tried ints. Although I realized persistence also works for
dicts ;')
def testid(newitem, K={}):
K[newitem] = newitem + 'item'
return 'the ID is', id(K), K
>>> testid('bonk')
('the ID is', 18263656, {'bonk': 'bonkitem'})
>>> testid('clonk')
('the ID is', 18263656, {'bonk': 'bonkitem', 'clonk': 'clonkitem'})
>>> testid('spam')
('the ID is', 18263656, {'bonk': 'bonkitem', 'clonk': 'clonkitem', 'spam':
'spamitem'})
--
Jim
"What a rotten, failed experiment. I'll start over. Maybe dogs instead of
monkeys this time." --God
More information about the Tutor
mailing list