Why such a result?

def f(a, x) : return a.x + x class A: def f(self, x) : self.x = x return x+1 a = A() y = f(a, a.f(2)) print(y) >>>5 I understand how we get 3, but then got confused

8/13/2019 4:57:50 AM

Михаил Горчанюк

2 Answers

New Answer


Already understood))). After the function f of class A, a.x is executed, the value is assigned 2. The same thing happens as in __init__, only the initialization is done manually


a = A() initially gives it the value 1. Then y = f(a, a.f(2)) gives y = f(1, a(2)) which is y = f(1,4) which results in y = 5.