简体   繁体   English

Python程序中与命名空间和范围相关的混乱

[英]Confusion in an Python program related to topic of Namespace and Scope

I just stepped in python recently and now I reached to the topic of Namespace and Scope. 我最近刚接触python,现在到达了命名空间和范围的主题。 With the intention of further understanding of this subject, I found this which contains a program resulting in my confusion. 为了进一步理解该主题, 我发现其中包含一个引起我困惑的程序。

And, here's the program: 而且,这是程序:

1  def outer_function():
2      a = 20
3      def inner_function():
4          a = 30
5          print('a =',a)
6
7      inner_function()
8      print('a =',a)
10     
11 a = 10
12 outer_function()
13 print('a =',a)

And here's the result which you must be quite sure but I am not 这是您必须十分确定的结果,但我不是

a = 30
a = 20
a = 10

but the output in my mind should be 但我心目中的输出应该是

a = 30
a = 30
a = 20
a = 10

Even I modified the original code to be like this in order to make it clearer: 为了使内容更清晰,我甚至将原始代码修改为:

def outer_function():
    a = 20
    def inner_function():
        a = 30
        print('a1 =',a)

    inner_function()
    print('a2 =',a)

a = 10
outer_function()
print('a3 =',a)

But it still like this: 但是它仍然像这样:

a1 = 30
a2 = 20
a3 = 10

So, the reason why I thought that there's supposed to a double a1 = 30 is because I believed that line5 in the code was executed twice as, 所以,我之所以认为应该将a1 = 30加倍,是因为我认为代码中的line5被执行了两次,

firstly, it runs when the outer_function() is called at line12 which goes from line1 to line8 and line5 is called with the a1 = 30 output 首先,它运行时outer_function()在被称为line12从变为line1line8line5被称为与a1 = 30输出

secondly, the line7 is called which triggered the line5 again with the same output as above, 其次, line7被称为触发了line5再次用相同的输出如上述,

finally the function outer_function() ends when line8 is done, that's when a2 = 20 show up. 最后,函数outer_function()在第line8行完成时结束,即当a2 = 20出现时。

Then go back to "main thread" (I just call it as this not actually mean it, or it is?) line13 and print a3 = 10 然后回到“主线程”(我只是称呼它,因为这实际上不是这个意思,或者是吗?)第line13并打印a3 = 10

So, as what I got is not what I thought, what's wrong with my understanding of this program? 那么,由于我所得到的不是我的想法,所以我对该程序的理解有什么问题?

Thanks for spending time on reading my problem, it will be of great help to me if you can give me a hand on this :) Thanks in advance 感谢您花时间阅读我的问题,如果您能帮助我,这将对我有很大帮助:)预先感谢

Edit: 编辑:

I just found out where my confusion was! 我才发现我的困惑在哪里! Great appreciation to those who give me a hand. 非常感谢那些帮助我的人。 Turns out the main problem of my confusion is my unstable foundation of the programming knowledge. 原来我困惑的主要问题是我对编程知识的了解不够稳定。 So, I incorrectly thought the line5 will run when the inner_function() was declared. 因此,我错误地认为line5将在声明inner_function()时运行。 Hahaha, how could this happen?? 哈哈哈,这怎么可能发生? This so basic mistakes just happened -- the declaration of the function will never run until it is called in the program! 这样的基本错误就发生了-函数的声明在程序中被调用之前将永远不会运行! That's why there's only one a = 30 ! 这就是为什么只有一个a = 30

Anyway, I will keep in mind that do not make the same mistakes again. 无论如何,我会记住不要再犯同样的错误。 I'm appreciated to those who give a hand on this minor basic syntactic mistake! 感谢那些帮助您解决此基本语法错误的人!

Cheers! 干杯!

The inner_function was declared on line 3, but it is only ever called once , and that's on line 7. inner_function在第3行中声明,但仅被调用过一次 ,在第7行中。

If you took out line 7, a1 would never print. 如果取出第7行, a1将永远不会打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM