-1

How can I make methods to apply += or -= or any other operators to change the private integer attribute of a class? I mean, setter decorator seems to just set the value like this:

@var.setter
def var(self, value):
    self.__var = value

But if I'd need to have multiple methods in which I have to change this property by value and not change it completely? Like this:

class SomeClass:
    def __init__(self, var):
        self.__var = var
    
    @property
    def var():
        return __var
 
    def VarPlus(self, value):
        self.__var += value

    def varMinus(self, value):
        self.__var -= value

It seems that I couldn't use a setter decorator hereabout, without it, I can't set new values to a private property

  • Does this answer your question? [Is there a way to overload += in python?](https://stackoverflow.com/questions/728361/is-there-a-way-to-overload-in-python) – Kraigolas May 28 '23 at 12:29

1 Answers1

0

First of all, you need to fix your var() method to be:

@property
def var(self):
    return self.__var

After that put attention on you method names (python is case sensitive, and your methods are named inconsistently: varMinus and VarPlus)

And now, if you want to implement your methods as operators, please use Python operator overloading

Daniel Zin
  • 479
  • 2
  • 7