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

Recommended Answers

All 4 Replies

Yes

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):
    OBJS.append(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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.