0

I am working on one of my first projects and I've got a class which has couple of instance variables. So I need to create a function that will add a given number to some of those variables. I am very new to python, so I'm not even sure if I can do that.

class B:

    def __init__(self, arg1=0, arg2=0):
        self.arg1 = arg1
        self.arg2 = arg2

    def do_something(self, argument, value):
        self.argument += value

    def __repr__(self):
        return f"{self.arg1=} {self.arg2=}"


user = B()
user.do_something('arg1', 20)
user.do_something('arg2', 30)
print(user)

This is not what my project will include, it just some kind of pattern.

So, back to my problem. Of course, I want to see 'self.arg1=20 self.arg2=30' in my console, but I don't know how to achieve the desired result.

I know that when I type 'self.argument' compiler thinks I want to add some value to variable argument (which, of course, doesn't exist). I feel like I can do that but don't have enough knowledge to do this.

I also have to say that I don't want to create a function for each variable, because I can have much more instance variables in class than 2.

0 Answers0