Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please clarify variables
#4
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
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

Reply


Messages In This Thread
Please clarify variables - by tonycstech - Nov-25-2019, 02:42 AM
RE: Please clarify variables - by michael1789 - Nov-25-2019, 03:29 AM
RE: Please clarify variables - by ThomasL - Nov-25-2019, 12:37 PM
RE: Please clarify variables - by buran - Nov-25-2019, 02:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,806 Jun-14-2021, 09:32 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020