Oct-17-2016, 04:56 PM
(Oct-16-2016, 07:46 PM)sunhear Wrote: i am trying to implement the changes in the code. I am new at programming objects and classes.
The original question was how to right a function/method to add 2 variables together when only 1 is passed as an argument.
By using "self" to reference the other instance variables in your object. Here, we create an object, then instantiate it, and call a method of that instance, passing only one argument. So we do math with two variables, even though we only passed one.
>>> class Spam(object): ... def __init__(self): ... self.eggs = 42 ... def add(self, other): ... return self.eggs + other ... >>> fork = Spam() >>> fork.add(9) 51