Question

If I have a class of the following format:

class TestClass:
  def __init__(self):
    self.value = 0

  def getNewObject(self):
    return TestClass()

Is there a limitation to the amount of times I can call the function? For example:

obj = TestClass()
obj.getNewObject().getNewObject()

Is there a limitation to how many times I can call getNewObject() on the return value of getNewObject()? If so what factors affect this?

Was it helpful?

Solution

I doubt it. One reason that makes me doubt it is that if we have this function:

def test(obj):
    obj.getNewObject().getNewObject().getNewObject()

And we disassemble it:

import dis
dis.dis(test)

We get this:

  2           0 LOAD_FAST                0 (obj)
              3 LOAD_ATTR                0 (getNewObject)
              6 CALL_FUNCTION            0
              9 LOAD_ATTR                0 (getNewObject)
             12 CALL_FUNCTION            0
             15 LOAD_ATTR                0 (getNewObject)
             18 CALL_FUNCTION            0
             21 POP_TOP             
             22 LOAD_CONST               0 (None)
             25 RETURN_VALUE        

That's just repetitions of LOAD_ATTR followed by CALL_FUNCTION. I can't imagine that that would require much memory or other resources to manage. As such, there is probably no limit.

OTHER TIPS

There is a recursion limit in Python (adjustable), but that is unrelated. Each call is made after the previous call has completed, so they're all called from the same level of the stack frame (i.e. from your user code). Now, you might hit a line-length limit or something, especially for an interactive Python shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top