Method is considering “self” as an argument variable

I have two classes, in the second class I defined a method that instantiates an object from the first class, assigns a value to one of its attribute, and then prints it.

The problem is when I call the function using:


I get the error.

TypeError: Pprint() missing 1 required positional argument: 'x'

My code:

class node():
    def __init__(self, test):
        self.test = test
class test():
    def Pprint(self, x):
        rootnode = node(x)

When I deleted the keyword self everything worked as intended. As far as I know self shouldn’t be considered an argument. What’s the problem?


This is python magic. When called from the class object test.Pprint(x) its a function requiring 2 arguments. When called from an instance, test().Pprint(x) python converts it to a method and automatically adds a reference to the instance as the first parameter. In your case, Pprint doesn’t use any of the functionality of the class, so it can be a static method.

class test:
    def Pprint(x):
        rootnode = node(x)

Now it works either from the class or an instance

t = test()