non-owning references?

U

Utpal Sarkar

Hi,

I'm not sure the subject describes what I'm looking for, but the
question is the following:
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?
I want this for what is essentially a singleton class, so that 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.

Thanks,

Utpal
 
G

Gerhard Häring

Utpal said:
Hi,
[...]

You're looking for the weakref module.

What you're describing there sounds like a nice exercise, but I cannot
imagine why you'd really need to clean it up, if it really is a singleton.

-- Gerhard
 
D

Doetoe

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


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
 
H

Hrvoje Niksic

Ben Finney said:
Python doesn't have “pointers”, and doesn't really have “variables”
either, at least not how many other languages use that term.

The OP didn't use the term "pointer", but the word "pointing", which
makes sense in the context. The term "variable" is used in the Python
language reference and elsewhere, and is quite compatible with how other
popular languages (Java, PHP, Lisp, ...) use it. Please stop
complaining about valid terminology; it is not helpful.
 
R

Rhodri James

The OP didn't use the term "pointer", but the word "pointing", which
makes sense in the context. The term "variable" is used in the Python
language reference and elsewhere, and is quite compatible with how other
popular languages (Java, PHP, Lisp, ...) use it.

Only superficially. Treating Python variables the same as C variables
(say) is one of the classic ways that newbies come unstuck when mutable
objects appear on the scene. While the OP appears to have the right idea,
your "correction" here could be quite misleading.
 
P

Piet van Oostrum

RJ> Only superficially. Treating Python variables the same as C variables
RJ> (say) is one of the classic ways that newbies come unstuck when mutable
RJ> objects appear on the scene. While the OP appears to have the right idea,
RJ> your "correction" here could be quite misleading.

If you read the OP, it is clear that he talked about a class variable,
which is a perfectly legal notion in Python, and is mentioned as such in
the language reference manual:
`Variables defined in the class definition are class variables'

And who was talking about C variables?
 
R

Rhodri James

If you read the OP, it is clear that he talked about a class variable,
which is a perfectly legal notion in Python, and is mentioned as such in
the language reference manual:
`Variables defined in the class definition are class variables'

Yes. I didn't think I needed to say that explicitly.
And who was talking about C variables?

Hrvoje, implicitly. 'The term "variable" is used in the Python
language reference and elsewhere, and is quite compatible with how
other popular languages (Java, PHP, Lisp, ...) use it.' I listed
C as another example of a popular language because I am very familiar
with how C's variables work; I don't know Java, I've never programmed
PHP in anger and it's twenty years since I last touched Lisp.

The point was, and remains, that this newsgroup gets regular traffic
from people who expect Python's variables to act like C's variables,
demonstrating that describing them as "quite compatible" is somewhat
misleading.
 
S

Steven D'Aprano

The term "variable" is used in the Python
language reference and elsewhere, and is quite compatible with how other
popular languages (Java, PHP, Lisp, ...) use it. Please stop
complaining about valid terminology; it is not helpful.

No, the use of the single term "variable" to describe two distinct
program models is not helpful. Whether other languages muddy the water
between memory-location based variables and name-binding is irrelevant to
whether we should do so. And quite a few of us are disappointed that the
Python language reference should confuse the issue by using misleading
terminology.

Unfortunately, the use of "variable" is so ingrained, and so simple
compared to name binding terminology, that I fear we'll never eradicate
it. I know sometimes I use it myself, but always with a little shiver of
shame that I'm misusing terminology.
 
T

Terry Reedy

Steven said:
No, the use of the single term "variable" to describe two distinct
program models is not helpful. Whether other languages muddy the water
between memory-location based variables and name-binding is irrelevant to
whether we should do so. And quite a few of us are disappointed that the
Python language reference should confuse the issue by using misleading
terminology.

Unfortunately, the use of "variable" is so ingrained, and so simple
compared to name binding terminology, that I fear we'll never eradicate
it. I know sometimes I use it myself, but always with a little shiver of
shame that I'm misusing terminology.

Some years ago, I read a claim that 'variable' has about 15 shades of
meaning in math (some referring to non-variable constants), making it
probably the most overloaded term in math. I am trying to mostly avoid
it in the book I am writing.

tjr
 
P

Piet van Oostrum

Rhodri James said:
RJ> The point was, and remains, that this newsgroup gets regular traffic
RJ> from people who expect Python's variables to act like C's variables,
RJ> demonstrating that describing them as "quite compatible" is somewhat
RJ> misleading.

So let' study these postings carefully to see what the actual confusion
is. I myself think that the confusion is not so much the variable
concept but the vale vs. reference semantics of objects. As such the
same thing plays a role in Java. Although the binding mechanisms in Java
and Python are different I don't think the actual semantics are so much
difference as to cause much confusion (with C it could be more). But
such a study could reveal that.

BTW. Which postings from (say) the last month did you have in mind?
 
C

Carl Banks

Yes. It should also be abundantly clear from the constant stream of
confused newbies on this point that its usage of that term is different
to what many expect from usage elsewhere.

Personally, I haven't noticed a constant stream, just a few here and
there.

I expect that someone used to C variables will expect Python variables
to behave the same way even if you call them something different, so
what really is the point?


Carl Banks
 
S

Steven D'Aprano

Personally, I haven't noticed a constant stream, just a few here and
there.

I expect that someone used to C variables will expect Python variables
to behave the same way even if you call them something different, so
what really is the point?

From time to time we get newbies to Python assuming that len(list) is
O(N), because the lists they learned about in Comp Sci 101 (or Lisp) are
linked lists and traversing a linked list to count the items is O(N). Do
you believe that if Python lists were called "arrays", or "waskilators",
they would make the same mistake?

[Generalisation]
C programmers have a mental model for how "variables" behave. They have
no such mental model for how "objects bound to names" behave, and so they
are less likely to jump to conclusions about the behaviour of name-
bindings. So I think you are mostly wrong.

However, only mostly -- human beings are generalisers par excellence, or
rather, *over*-generalisers par excellence. Experienced coders are likely
to rapidly realise that name-binding is very similar to the C variable
model, and some percentage of them will over-generalise to assume that
name-binding is the same as the C model. But if they do, at least that
will be their fault for jumping to conclusions, not our fault for
misleading them.
 
J

John Nagle

Peter said:

Yes. Weak references can be quite useful. I have a version of
BeautifulSoup in which all the "upward" and "backwards" links are
weak references, but the "downward" and "forwards" links are strong
references. This breaks the loops in the data structure, and thus
trees and subtrees will go away when no longer required, without
requiring a GC cycle.

John Nagle
 
E

Emmanuel Surleau

Yes. It should also be abundantly clear from the constant stream of
confused newbies on this point that its usage of that term is different
to what many expect from usage elsewhere.


I disagree with your assertions.

Rather than yet another round of this tedious debate, I merely point
interested readers to <URL:http://effbot.org/zone/python-objects.htm>
and ask them to draw their own conclusion on how compatible their
pre-existing “variable†concept is with Python's object reference
model.

Then I must be one of these "confused newbies". After a cursory look at your
article, I don't see a difference with Java variables (as long as we speak
about non-primitive types).

Cheers,

Emm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top