class Class(): def __init__(self, var): self.var = var def method(self, var1=self.var): print(var1) Closed due to the fact that the essence of the issue is incomprehensible by the participants Dmitry Kozlov , aleksandr barakin , 0xdb , LFC , Suvitruf ♦ January 27 at 8:04 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
|
1 answer
Error due to the fact that you pass self.var as an argument to the function. If you want to access the var variable that you defined in the class constructor, then you do not need to pass it to other methods of the class as a separate argument - you will have access to it through self , which is the first argument of the method. Try this:
class Class(): def __init__(self, var): self.var = var def method(self, var2): return self.var + var2 obj = Class(333) print(obj.method(111)) print(obj.var) The result is:
444 333 - Later, I want to pass an argument to the method. Therefore, I need to specify it. - Infidus
- Completed the answer by adding an argument. - Andrey
|