TypeError: A_class_meth() missing 1 required positional argument: 'A_class_meth_var1'

837 Views Asked by At

I'm getting the following error for my code:

TypeError: A_class_meth() missing 1 required positional argument: 'A_class_meth_var1'.

What should be modify in "A.A_class_meth"?

class A():
    def __init__(self,init_var):
        self.init_var = init_var

    def A_class_meth(self, A_class_meth_var1):
        print("run")

class B():
    def Start(self):        
        self.B_class_var = A("NAME")
        A.A_class_meth(self.B_class_var)

var = B()
var.Start()
1

There are 1 best solutions below

0
On BEST ANSWER

Create and store an instance of your A class inside the start() method and then reference that.

Otherwise, the self.B_class_var is getting passed as the self parameter

class A():
    def __init__(self,init_var):
        self.init_var = init_var

    def A_class_meth(self, A_class_meth_var1):
        print("run")

class B():
    def start(self):  
        a = A("NAME")      
        self.B_class_var = a
        a.A_class_meth(self.B_class_var)

var = B()
var.start()

>> run

or else, alternatively, as suggested above, make the A_class_meth a static method and remove the self parameter:

class A():
    def __init__(self,init_var):
        self.init_var = init_var

    @staticmethod
    def A_class_meth(A_class_meth_var1):
        print("run")

class B():
    def start(self):     
        self.B_class_var = A("NAME")
        A.A_class_meth(self.B_class_var)

var = B()
var.start()

>> run