class Demo:
var = 10
def add(self, addNum):
print(f'adding {addNum} and {self.var} ')
return self.var + addNum
class Demo2:
def __init__(self, objref):
var = 2
self.objref = objref
def increment(self):
self.objref.var += 1
def define_Callback(self, callback):
self.callback = callback
if __name__ == "__main__":
# this is the same as js / etc / most langs
obj = Demo()
obj2 = Demo2(obj)
obj2.increment()
obj2.define_Callback(obj.add)
print('obj.var is', obj.var)
var = obj2.callback(5)
print('var + 5 = ', var)
--- output ---
obj.var is 11
adding 5 and 11
var + 5 = 16
class Demo {
var = 10;
add(num2Add) {
console.log(`adding ${num2Add} to ${this.var}`);
this.var += num2Add;
return this.var;
}
}
class Demo2 {
constructor(objref) {
this.objref = objref;
this.var = 5;
this.callback = null;
}
increment() {
this.objref.var++;
}
defineCallback(callback) {
this.callback = callback;
}
}
var obj = new Demo();
var obj2 = new Demo2(obj);
obj2.increment();
obj2.defineCallback(obj.add);
console.log('obj.var is ' + obj.var);
var newvar = obj2.callback(5);
console.log(`var (${obj.var}) + 5 isn't ${newvar}`); // 16
--- output ---
obj.var is 11
adding 5 to 5
var (11) + 5 isn't 10
Realize it is what it is, but this is mostly what screws me up