Why is the python destructor being called?
python
Solution
The output in question is replicated only in interactive shell.
In interactive session, additional variable `_` exists, which refer to last value.
Using sys.getrefcount to inspect reference count:
>>> import sys
>>> class SmartPhone:
... def __del__(self):
... print "destroyed"
...
>>> y = SmartPhone()
>>> sys.getrefcount(y) # not printed, _ does not reference SmartPhone object yet.
2
>>> y
<__main__.SmartPhone instance at 0x000000000263B588>
>>> sys.getrefcount(y) # y printed, _ reference SmartPhone object.
3
`2`, `3` in above output should be `1`, `2`. They are printed that way, because getrefcount() increase reference count temporarily as mentioned in getrefcount documentation.
I changed SmartPhone as follow to make it easy to inspect what's going on.
>>> class SmartPhone(object):
... def __init__(self, name):
... self.name = name
... def __repr__(self):
... return super(SmartPhone, self).__repr__() + ' name=' + self.name
... def __del__(self):
... print "destroyed", self
...
>>> y = SmartPhone('first')
>>> del y # deleted immediately, because only "y" reference it.
destroyed <__main__.SmartPhone object at 0x00000000024FEFD0> name=first
>>> y = SmartPhone('second')
>>> y # at this time, _ reference to second y (y's reference count is now 2)
<__main__.SmartPhone object at 0x00000000024FEFD0> name=second
>>> y
<__main__.SmartPhone object at 0x00000000024FEFD0> name=second
>>> y
<__main__.SmartPhone object at 0x00000000024FEFD0> name=second
>>> del y # not deleted immediately, because _ reference it.
>>> y = SmartPhone('third') # _ still reference the second y, because nothing is printed.
>>> y # second y is deleted, because _ now reference the third y. (no reference to the second y)
destroyed <__main__.SmartPhone object at 0x00000000024FEFD0> name=second
<__main__.SmartPhone object at 0x000000000264A470> name=third
Problem
When I type this into the interpreter, calling 'y' seems to invoke the destructor? ``` class SmartPhone: def __del__(self): print "destroyed" y = SmartPhone() y #prints destroyed, why is that? y #object is still there ``` Here is one run, output does not make sense to me. ``` C:\Users\z4>python Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> class SmartPhone: ... def __del__(self): ... print "destroyed" ... >>> y = SmartPhone() >>> del y destroyed >>> y = SmartPhone() >>> y <__main__.SmartPhone instance at 0x01A7CBC0> >>> y <__main__.SmartPhone instance at 0x01A7CBC0> >>> y <__main__.SmartPhone instance at 0x01A7CBC0> >>> del y >>> y = SmartPhone() >>> y destroyed <__main__.SmartPhone instance at 0x01A7CB98> >>> ``` and another, calling 'del y' sometimes calls the destructor and sometimes doesn't ``` C:\Users\z4>python Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> class SmartPhone: ... def __del__(self): ... print "destroyed" ... >>> >>> y = SmartPhone() >>> >>> y <__main__.SmartPhone instance at 0x01B6CBE8> >>> y <__main__.SmartPhone instance at 0x01B6CBE8> >>> y <__main__.SmartPhone instance at 0x01B6CBE8> >>> del y >>> y Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'y' is not defined >>> y = SmartPhone() >>> y destroyed <__main__.SmartPhone instance at 0x01B6CC38> >>> del y >>> y Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'y' is not defined >>> ```