non-owning references?

Doetoe doetoex at gmail.com
Fri Jul 24 15:18:48 CEST 2009


On Jul 24, 3:06 pm, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
> Utpal Sarkar <doe... at gmail.com> writes:
> > Is there a way I can tell a variable that the object it is pointing
> > too is not owned by it, in the sense that if it is the only reference
> > to the object it can be garbage collected?
>
> Python doesn't have “pointers”, and doesn't really have “variables”
> either, at least not how many other languages use that term.
>
> What it does have is references to objects
> <URL:http://effbot.org/zone/python-objects.htm>.
>
> > on first instantiation the object is created and a reference is kept
> > in the class, that is used to return the same object in subsequent
> > instantiations. When all instances go out of scope, the reference in
> > the class is still there, preventing it from being garbage collected,
> > but since the instance can be huge, I would like it to be.
>
> What you are asking for is called a “weak reference” and is provided
> by the ‘weakref’ module <URL:http://docs.python.org/library/weakref>.
>
> --
>  \      “Patience, n. A minor form of despair, disguised as a virtue.” |
>   `\                   —Ambrose Bierce, _The Devil's Dictionary_, 1906 |
> _o__)                                                                  |
> Ben Finney

Thanks to the three of you. This is precisely what I needed!
Gerhard, the reason I need to clean it up is that it is a lazy data
structure that can grow to arbitrary size. When it is not needed
anymore it would still remain in memory.

Utpal



More information about the Python-list mailing list