0

If I get the id of an object, how can I delete an object by it's id ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bussiere
  • 500
  • 13
  • 60
  • 119
  • How did you get the id? You called `id(someObject)`, right? You will also do `del someObject`. How is it possible to have the id, but not the object? Can you provide some context or code sample? – S.Lott Jan 27 '11 at 14:48

4 Answers4

2

This is not a good idea. Try using the weakref module, which allows you to create weak references (analogous to symbolic links) to objects.

1

AFAIK objects are deleted by garbage collector in python. You can't force a delete yourself.

gruszczy
  • 40,948
  • 31
  • 128
  • 181
  • Not strictly true; that's what the `del` keyword is for. – nmichaels Jan 27 '11 at 14:44
  • 9
    No, that's not deleting object, but deleting a variable. Totally different thing. – gruszczy Jan 27 '11 at 14:46
  • Delete the variable. The reference count goes to zero. The object can be removed, then, also. – S.Lott Jan 27 '11 at 15:55
  • You don't have to wait for the next cycle of garbage collector? – gruszczy Jan 27 '11 at 16:50
  • 1
    You don't delete a variable in Python, you delete a name aka reference to an object; Python doesn't have variables in the sense of other programming languages. In CPython, an objects gets deleted if its reference count reaches zero unless it's part of a cycle, so its deletion becomes responsibility of the garbage collector. – tzot Feb 21 '11 at 11:58
0

The id refers to a memory address. You "can't" delete (or free) a memory address, only its references, with the del statement

Fábio Diniz
  • 10,077
  • 3
  • 38
  • 45
-2

You can delete objects by calling del() on them. But AFAIK id won't help in that.

erickrf
  • 2,069
  • 5
  • 21
  • 44