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'
class node(): def __init__(self, test): self.test = test class test(): def Pprint(self, x): rootnode = node(x) print(rootnode.test)
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: @staticmethod def Pprint(x): rootnode = node(x) print(rootnode.test)
Now it works either from the class or an instance
test.Pprint(x) t = test() t.Pprint(x)