Lec4 concept of Scope: when the professor explains x.f1, it is 1st assigned with 3, so I assume it is somehow related to x.main. But for x.g, it is just simply assigned with 'abc', no relationship to x.f1 or x.main. Why is that? def f1(x): def g(): x.g = 'abc' x.f1 = x.f1 + 1 print 'X =', x g() assert False return x x.main = 3 z = f1(x)
The key difference between x.f1 and x.g is that f1 takes an argument. You can't call it without passing it an actual parameter. In this case, it takes x.main and does things with it. g however takes no argument (empty parens), so x.g indeed has no relationship with x.main or x.f1, it doesn't "modify" a preexisting variable. The professor did that in order to show that what happens within a scope stays in that scope (well, under certain conditions), and that all those x's really are different. Does that answer your question?
Very clear, thanks a lot
Join our real-time social learning platform and learn together with your friends!