Semantics (computer science)
This is because there is still a reference to the object from elements, but the object will never be accessed again through this reference, since elements is private to the class and the pop method only returns references to elements it has not already popped (once size is decremented, that element will never be accessed again by this class). However, this requires analysis of the code of the class, which is undecidable in general.
If a later push call re-grows the stack to the previous size, overwriting this last reference, then the object will become syntactic garbage, since it is unreachable, and will be eligible for garbage collection.
Automatic garbage collection
An example of the automatic collection of syntactic garbage, by reference counting garbage collection, can be produced using the Python command-line interpreter:
>> class Foo(object): ... 'This is an empty testing class.' ... pass ... >> bar = Foo >> bar >> del bar
In this session, an object is created, its location in the memory is displayed, and the only reference to the object is then destroyed—there is no way to ever use the object again from this point on, as there are no references to it. This becomes apparent when we try to access the original reference:
>> bar Traceback (most recent call last): File , line 1, in ? NameError: name 'bar' is not defined
As it is impossible to refer to the object, it has become useless: the object is garbage. Since Python uses garbage collection, it automatically deallocates the memory that was used for the object so that it may be used again:
>> class Bar(object): ... 'This is another testing class.' ... pass ... >> baz = Bar >> baz