Nested class versus scope

I am new to programming. Firstly, this is my code:

x = 11 def f(): x = 22 print(x) class C: print(x) f() 

I got three printed results, 22 22, as a conclusion. When I changed my code as follows:

 x = 11 def f(): x = 22 print(x) class C: print(x) x = 33 print(x) f() 

I got "22 11 33" instead of what I expect: "22 22 33."

It appears that when I add local x to the nested class, the order in which the variables are searched changes. I believe that some tricks about the clouds that I do not know yet. Can someone explain this to me?

+5
source share

Source: https://habr.com/ru/post/1273595/


All Articles