-2

I am debugging a Python code and see this code pattern in one of the classes:


def someOtherFunction (x):
    return x**2

class baseX():
    pass

class baseY():
    pass


class newBase (baseX, baseY):

    def __init__ (self, **kwargs):
        super().__init__(**kwargs)
    
    def func (self, varX):
        varA = someOtherFunction(varA)
        varB = self(varA)
        return varB

I am having a hard time understanding the self() syntax. What is varB = self(varA)? What is it doing?

The original code is not my code. The original code is supposed to work. The pseudo-code I wrote is to represent my question better. Please see the original code on Github (Line 101)

1 Answers1

1
  • First, you need provide a MRE. Such as when you defined a class, dont use def before class
  • I try to fix the bug in your code to a MRE
def someOtherFunction(x):
    return x+1

class baseX:
    pass

class baseY:
    pass

class newBase (baseX, baseY):

    def __init__ (self, someARGS):
        self.varA = someARGS

    def __call__(self,x):
        return x + 5

    def func (self, varX, varA):
        varX = someOtherFunction(varX)
        varB = self(varA)
        return varB

x = newBase(5)
print(x.func(0,10))

#result : 15
  • In this code, the self(varA) represent to call newBase as a callable object, which normal class is not callable.
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
  • 1
    Thank you SO much. I think you just nailed it! That __call__() method was my problem. It seems in one of the upper classes (please see the Github for the actual code) there is a __call__() method that is passed to the child class "newBase". And so it is now can be called. Thanks again. – Sajjad Kavyani Nov 12 '21 at 15:39