0

At first, forgive my poor english. I meet a problem. the code like this.

class Mydict(dict):
  pass

print id(Mydict({"a": 1, "b": 2}))
print id(Mydict({"a": 1, "b": 2}))
print Mydict({"a": 1, "b": 2}).pop("a")
print id(Mydict({"a": 1, "b": 2}))

output:

>>>139700506146328
>>>139700506146328
>>>1
>>>139700506146328

when I instantiate a class. and, no variable assign the instance. it give me the some physical address.

what does python do when it instantiate? how does it allocate physical address?where can i get some information from ?

my python is Cpython 2.7.11

-----------------------add(2017-09-17) -------

I used pycharm.

in my opinion, it maybe give difference physical address.likes

>>>139700506146312
>>>139700506233123
>>>1
>>>139700506235222
jin
  • 430
  • 2
  • 9
  • There's nothing special about the address it's using. The reason it keeps giving the same address over and over is because after the instances are passed to `id`, there are no remaining references to them and the storage is being immediately reclaimed. If you keep a reference to one of the instances in a variable, then the next instance will have a different address. – Tom Karzes Sep 16 '17 at 08:31

1 Answers1

1

The object will ultimatively be destroyed by garbage collection. Since I assume you're on the REPL it is possible that it is not being destroyed because you're holding a reference to it with the _ underscore operator.

Also what do you expect python to do when you do not give a variable; refused to create an instance. The python interpreter is doing what its told to which is create an object and perform some operations on it and will ultimatively destroy it when gc is called.

See: Python garbage collector documentation

zython
  • 1,176
  • 4
  • 22
  • 50