pythonpython-3.xpositional-argument

missing 1 required positional argument when using self


I'm learning python. I have created a class visualizer which contains 2 methods. Now I want to call the first method inside my second method. I read I have to use self:

def method2(self):
    self.method1()

This works. Now I'm adding additional parameters:

def method2(self, param1, param2, param3):
    self.method1()

And I call it from my main.py:

xx.method2(param1, param2, param3)

Now I get an error:

missing 1 required positional argument: 'param3'

I checked, param3 is there and the sequence of parameters is the same. I think it's conflicting with self.

How can I solve this? Do I need an init method inside my class?

EDIT: In my Main:

from project.visualizer import Visualizer
vsl = Visualizer

vsl.method2(param1, param2, param3)

Solution

  • vsl = Visualizer
    

    This doesn't create an instance of Visualizer like you intend. As written it causes vsl to point to the Visualizer class rather than an instance of the class. When you call vs1.method2() it's as if you've written:

    Visualizer.method2(param1, param2, param3)
    

    It should be:

    vsl = Visualizer()
    

    If Visualizer's constructor takes parameters, pass those in.