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 8928473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:26:40+00:00 2026-06-15T08:26:40+00:00

Something crossed my mind recently in Python: x = y(z) is equivalent to x

  • 0

Something crossed my mind recently in Python: x = y(z) is equivalent to x = y.__call__(z). However, a test appears to invalidate that assumption and also leads to Python’s interpreter to crash.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def ret(*args):
...     return args
...
>>> ret(1, 2, 3)
(1, 2, 3)
>>> for _ in range(1000000):
...     ret = ret.__call__
...
>>> ret(1, 2, 3)

Running the second ret(1, 2, 3) leads Python to crash and return to the command prompt (image).

  1. What is happening in the background when the line ret = ret.__call__ executes?
  2. Why does Python stop working on the last line, and should it be reported as a bug?

Useless Reference: Python functions and their __call__ attribute

  • 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-06-15T08:26:41+00:00Added an answer on June 15, 2026 at 8:26 am

    You are creating a deeply nested structure of method wrappers. Each method wrapper still has a reference to self, where self is a reference to the parent method wrapper, all the way back to the original function:

    >>> ret, ret.__call__.__self__
    (<function ret at 0x10f17a050>, <function ret at 0x10f17a050>)
    >>> ret.__call__, ret.__call__.__call__.__self__
    (<method-wrapper '__call__' of function object at 0x10f17a050>, <method-wrapper '__call__' of function object at 0x10f17a050>)
    

    Note how the memory address of the method wrapper __self__ attribute points to the parent object.

    If you create enough of these wrappers, you could run out of memory.

    Python creates such wrappers for all functions bound to an instance. It’s the same for a custom class with a method:

    >>> class Foo: 
    ...     def bar(self): return
    ... 
    >>> Foo().bar
    <bound method Foo.bar of <__main__.Foo object at 0x10f1798d0>>
    >>> Foo().bar, Foo().bar.__self__
    (<bound method Foo.bar of <__main__.Foo object at 0x10f179710>>, <__main__.Foo object at 0x10f179850>)
    

    Methods are created from functions as needed, when accessing the method via attribute access. Because they hold a reference to self, they are retained in memory as long as you hold a reference to them. Your chain of references thus holds 100000 memory wrappers alive.

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

Sidebar

Related Questions

Something just crossed my mind, if i have a result set that is around
It just crossed my mind that it would be extremely nice to be able
Something that seems to be absent from the otherwise great new features for Windows
Something that I think is really strange is happening when I use die() .
I don't have a specific scenario in mind, but this question just crossed my
Suppose you want to write into a database that something is 30 meters long,
OK I have a mySQL Database that looks something like this ID - an
As something of an amateur Obj-C/Cocoa developer, I've recently been wrestling with enabling drag
Something I encountered today at work, I tried to make a view that used
Something that happens to me a lot while web programming: I want to run

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.