Python language problem

Steve Holden steve at holdenweb.com
Wed Jun 7 15:09:36 CEST 2006


ripley wrote:
> Boris Borcic wrote:
> 
>>ripleyfu at gmail.com wrote:
>>
>>>>>>class A:
>>>
>>>...   pass
>>>...
>>>
>>>>>>a = A()
>>>>>>b = a
>>>>>>del b
>>>>>>a
>>>
>>><__main__.A instance at 0x00B91BC0>
>>>I want to delete 'a' through 'b', why It does't?
>>>How can I do that?
>>
>>del a,b
> 
> 
> But 'b' is also deleted, i want use 'b' to delete 'a', 'b' is exists.
> 
You can't do what you want to do.

Python names are independent references to objects. The "del" statement 
deletes a name from a namespace (or an item from a structure), and 
cannot be used to delete all references to a given object. In general 
there's no way to delete a referenced object - we normally rely on the 
implementation (in CPython reference counting plus garbage collection, 
in other implementations just plain garbage collection) to perform the 
deletion when no live references to an object remain.

Perhaps you'd like to explain *why* you find a need to do this (in other 
words, what's your use case)?

Weak references are one possibility that might help you, but without 
knowing your real requirements it's difficult to be more helpful.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list