Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:30:42+00:00 2026-05-28T21:30:42+00:00

This is basically a question about the lifetime of temporaries. If a function returns

  • 0

This is basically a question about the lifetime of temporaries. If a function returns an object, but the reference is not assigned to a variable and is only used to call a method on the returned object, is the temporary reference automatically cleared?

To give a concrete example, suppose there is this chain of method calls:

o.method_a().method_b()

Is the temporary reference returned by o.method_a() automatically cleared when the call to method_b() finishes, as if the line were written like:

tmp = o.method_a()
try:
    tmp.method_b()
finally:
    tmp = None

I am interested in a general answer. CPython finalizes objects as soon as the reference count drops to 0. Other Python implementations might not finalize objects immediately.

I am wondering if the Python language is like C++, which guarantees that temporary objects are destroyed at the end of the statement for which they were created (except that in Python, the question is whether temporary references are cleared at the end of the statement for which they were created).

In C++, similar code might be implemented with:

class B {
public:
    void method_b();
};

class A {
public:
    std::shared_ptr<B> method_a();
};



A o;
o.method_a()->method_b();

The C++ standard states "Temporary objects are destroyed as the last step in evaluating the full-expression … that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception." In this example, it means that the temporary std::shared_ptr<B> object created by the call to A::method_a() is destroyed immediately at the end of evaluation of the full-expression o.method_a()->method_b();. Destroying a std::shared_ptr means clearing a reference to the shared object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:30:42+00:00Added an answer on May 28, 2026 at 9:30 pm

    Yes, garbage collector is in charge of keeping track of reference counts. When the reference count drops to zero, the garbage collector deletes the object. Here is a quick example.

    >>> class C(object):
    ...     def foo(self):
    ...          return B()
    ... 
    >>> class B(object):
    ...     def __del__(self):
    ...         print 'deleting %r' %self
    ...     def bar(self):
    ...         print 'I am method bar of %r' %self
    ... 
    >>> c = C()
    >>> c.foo().bar()
    I am method bar of <__main__.B object at 0xa187d2c>
    deleting <__main__.B object at 0xa187d2c>
    

    As a side note, if there is a reference to the temporary object from any other object, then it doesn’t get deleted. The object is garbage collected only when the reference count is zero.

    Also a note about del. del just drops the reference count of an object. It doesn’t delete an object. For example, if a = b = C(), del a only removes the name a and drops the reference count for object C(), but doesn’t really delete it, because b still refers to it.

    You’re right, reference counting is a CPython implementation. As far as other implementations go, Python specification doesn’t offer any guarantees about when objects are destroyed.

    This is what Python Language Reference has to say about the matter:

    Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not really sure how to title this question but basically I have an
This is basically a very noob question about Ruby console (IRB), but I couldn't
This is basically a duplicate of this question , but the accepted answer was
This is basically a question about the readability, style, performance of 2 different approaches
In this question I asked about NHibernate session lifetime. I'm using a desktop application,
This is a very open question about a problem that we have. Basically, we
Basically this is a follow up of this question about most vexing parse. I
I've asked a question about this class before, but here is one again. I've
First of all, this is not a question about how to get the user's
I've got a very simple question about a game I created (this is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.