What you have is not really a class.
OK, technically you use the word class when you define them. But they are in fact just another layer over regular functions. If you remove all the reference to calc your code will work all the same. The idea is that class is blueprint of an object. You can create as many instances of a class as you wish. To be a class it has to have access to instance and/or class itself. Methods would get a class instance as first argument (by convention we use self for that) You really need to get back to read about class basics/fundamentals.
Don't use global variables. You pass arguments to functions/methods and/or use class properties.
Using result property to store well, the result may work, but probably more convenient would be that your methods return the result
So there may be different implementations, but very basic example
OK, technically you use the word class when you define them. But they are in fact just another layer over regular functions. If you remove all the reference to calc your code will work all the same. The idea is that class is blueprint of an object. You can create as many instances of a class as you wish. To be a class it has to have access to instance and/or class itself. Methods would get a class instance as first argument (by convention we use self for that) You really need to get back to read about class basics/fundamentals.
Don't use global variables. You pass arguments to functions/methods and/or use class properties.
Using result property to store well, the result may work, but probably more convenient would be that your methods return the result
So there may be different implementations, but very basic example
class Calculator: def __init__(self): self._result = 0 # by convention undescore means for internal use variable @property def result(self): return self._result def add(self, n): self._result += n def substract(self, n): self._result -= n def clear(self): self._result = 0 calc = Calculator() # create an instance of the class print(calc.result) calc.add(5) # result = 5 print(calc.result) calc.substract(1) # result = 4 calc.substract(3) # result = 1 print(calc.result) calc.clear() print(calc.result)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs