3 Years
Discussion Span
Last Post by ~s.o.s~

EDIT: The below explanation assumes someboundmethod is actually called instead of just storing the method reference in d. If d stores the method reference instead of the result of the method call, foo instance will surely hang around as long as d is reachable.

If there's a class called foo and you do d = foo().someboundmethod, will the instance of foo stil be there?

Still around? Maybe.

It is possible that the someboundmethod might add the foo instance to a global collection in which case it might stick around. Something like:

OBJS = []

class Foo(object):

  def somemethod(self):

If not the above case, it might still be around but not reachable. Do note that this depends a lot on the Python implementation in consideration so it's safe to assume that as long as an object is referred by some "live" object, it will stick around. If not, it it bound to get garbage collected sooner or later.

Edited by ~s.o.s~


~s.o.s~, I think you overlooked the fact that someboundmethod is never actually called in the james' code. d simply stores a reference to the method someboundmethod of the object returned by foo(). Since that method is bound to the object, the object will live at least as a long as the method is referred to.


+1; you are correct indeed. I assumed it was a method call. I'll edit my original post to mention that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.