non-owning references?

Discussion in 'Python' started by Utpal Sarkar, Jul 24, 2009.

  1. Utpal Sarkar

    Utpal Sarkar Guest

    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
     
    Utpal Sarkar, Jul 24, 2009
    #1
    1. Advertising

  2. Utpal Sarkar

    Peter Otten Guest

    Utpal Sarkar wrote:

    > 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?


    http://docs.python.org/library/weakref.html
     
    Peter Otten, Jul 24, 2009
    #2
    1. Advertising

  3. Utpal Sarkar wrote:
    > 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
     
    Gerhard Häring, Jul 24, 2009
    #3
  4. Utpal Sarkar

    Doetoe Guest

    On Jul 24, 3:06 pm, Ben Finney <> wrote:
    > Utpal Sarkar <> 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
     
    Doetoe, Jul 24, 2009
    #4
  5. Ben Finney <> writes:

    > Utpal Sarkar <> 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.


    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.
     
    Hrvoje Niksic, Jul 24, 2009
    #5
  6. Utpal Sarkar

    Rhodri James Guest

    On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic <>
    wrote:

    > Ben Finney <> writes:
    >
    >> Utpal Sarkar <> 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.

    >
    > 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.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jul 24, 2009
    #6
  7. >>>>> "Rhodri James" <> (RJ) wrote:

    >RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic <> wrote:
    >>> Ben Finney <> writes:
    >>>
    >>>> Utpal Sarkar <> 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.
    >>>
    >>> 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.


    >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?
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 24, 2009
    #7
  8. Utpal Sarkar

    Rhodri James Guest

    On Fri, 24 Jul 2009 16:03:58 +0100, Piet van Oostrum <> wrote:

    >>>>>> "Rhodri James" <> (RJ) wrote:

    >
    >> RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic
    >> <> wrote:
    >>>> Ben Finney <> writes:
    >>>>
    >>>>> Utpal Sarkar <> 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.
    >>>>
    >>>> 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.

    >
    >> 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'


    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.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jul 24, 2009
    #8
  9. On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote:

    > 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.



    --
    Steven
     
    Steven D'Aprano, Jul 24, 2009
    #9
  10. Utpal Sarkar

    Terry Reedy Guest

    Steven D'Aprano wrote:
    > On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote:
    >
    >> 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.


    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
     
    Terry Reedy, Jul 24, 2009
    #10
  11. >>>>> "Rhodri James" <> (RJ) wrote:

    >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?
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 25, 2009
    #11
  12. Utpal Sarkar

    Carl Banks Guest

    On Jul 24, 8:14 am, Ben Finney <> wrote:
    > Hrvoje Niksic <> writes:
    > > The term "variable" is used in the Python language reference and
    > > elsewhere

    >
    > 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
     
    Carl Banks, Jul 25, 2009
    #12
  13. On Sat, 25 Jul 2009 04:45:48 -0700, Carl Banks wrote:

    > On Jul 24, 8:14 am, Ben Finney <> wrote:
    >> Hrvoje Niksic <> writes:
    >> > The term "variable" is used in the Python language reference and
    >> > elsewhere

    >>
    >> 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?


    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.


    --
    Steven
     
    Steven D'Aprano, Jul 25, 2009
    #13
  14. Utpal Sarkar

    John Nagle Guest

    Peter Otten wrote:
    > Utpal Sarkar wrote:
    >
    >> 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?

    >
    > http://docs.python.org/library/weakref.html


    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
     
    John Nagle, Jul 26, 2009
    #14
  15. On Friday 24 July 2009 17:14:06 you wrote:
    > Hrvoje Niksic <> writes:
    > > The term "variable" is used in the Python language reference and
    > > elsewhere

    >
    > 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.
    >
    > > and is quite compatible with how other popular languages (Java, PHP,
    > > Lisp, ...) use it. Please stop complaining about valid terminology; it
    > > is not helpful.

    >
    > 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
     
    Emmanuel Surleau, Jul 26, 2009
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob
    Replies:
    1
    Views:
    552
    Andy Fish
    Jul 31, 2003
  2. Marijn
    Replies:
    2
    Views:
    402
    Marijn
    Aug 8, 2003
  3. Roger Leigh
    Replies:
    8
    Views:
    474
    Karl Heinz Buchegger
    Nov 17, 2003
  4. dkmd_nielsen

    Determine "owning" class?

    dkmd_nielsen, Mar 9, 2008, in forum: Ruby
    Replies:
    6
    Views:
    101
    Pit Capitain
    Mar 12, 2008
  5. K. Frank
    Replies:
    4
    Views:
    190
Loading...

Share This Page