【问题标题】:understanding the execution flow in python了解python中的执行流程
【发布时间】:2017-02-02 05:25:04
【问题描述】:

刚接触python,对执行流程感到困惑:

为了详细说明,我在说明以下示例:

示例 1:

def hello():
    print("hello world")
    python()

def python():
    print("testing main")

if __name__ == "__main__":
    hello()

**output :**
    hello world
    testing main

注意:我知道 __name__ == "__main__" 的用法。

示例 2:

python()

def python():
    print("testing main")

**output**
File "main_flow.py", line 2, in <module>
python()
NameError: name 'python' is not defined

据我所知,python 是按顺序执行程序的(如果我错了,请纠正我),因此在示例 2 中遇到 python() 方法时无法找到它。

我的困惑是为什么在示例 1 中没有发生类似的错误,这种情况下的执行流程是什么。

【问题讨论】:

  • 因为在第一个例子中,python在定义之前没有被执行,所以在第二个例子中,它是。

标签: python python-2.7


【解决方案1】:

编辑 1: 当您在 python 中调用自定义函数时,它必须知道它在文件中的位置。我们使用def function_name(): 来定义我们在脚本中使用的函数的位置。我们必须在调用function_name() 之前调用def function_name():,否则脚本不会知道function_name() 并会引发异常(函数未找到错误)。

通过运行def function_name():,它只会让脚本知道有一个名为function_name() 的函数,但它实际上不会运行function_name() 中的代码,直到你调用它。

在第二个示例中,您在脚本到达 def python() 之前调用了 python(),因此它还不知道 python() 是什么。

示例 1 顺序为:

1.    def hello(): # Python now knows about function hello()
5.        print("hello world")
6.        python()

2.    def python(): # Python now knows about function python()
7.        print("testing main")

3.    if __name__ == "__main__":
4.       hello() 

示例 2 顺序为:

1.    python()    # Error because Python doesn't know what function python() is yet

-     def python(): # Python doesn't reach this line because of the above error
-         print("testing main")

示例 2 解决方案是:

1.     def python(): # Python now knows about function python()
3.         print("testing main")

2.     python()   

编辑 2: 从脚本的角度重申示例 1

def hello(): 
def python(): 
if __name__ == "__main__":
hello() 
print("hello world")
python()
print("testing main")

这是脚本将看到和运行每一行代码的顺序。很明显,脚本知道 python(),因为 def 在第 2 行被调用,python() 在第 6 行被调用。

您似乎不了解定义范围的含义。阅读它。函数的作用域在def期间不执行,只在函数调用时执行。

【讨论】:

  • 查看编辑。如果您需要进一步解释,请告诉我。
  • 我同意并理解这一点,“我们必须调用 def function_name():在我们调用 function_name() 之前,否则脚本不会知道 function_name() 并会引发异常(函数不发现错误)。”但是在示例 1 中也发生了同样的情况,该函数 python() 在定义之前被调用,为什么在这种情况下没有发生相同的错误。不要介意再次问同样的问题。
  • Example 1的调用顺序,很明显顺序是1.def hello():,2 . def python():, 3. if __name__ == "__main__":, 4. hello() 等我已经在我的解决方案中写了顺序,应该很漂亮如果你按照顺序清除。
  • 感谢您详细解释,真的,我对范围的印象不同..
【解决方案2】:

不是关于__name__ == '__main__',而是因为第二个示例中的函数调用是在定义函数之前执行的。

您的第一个示例执行函数python定义所有函数之后,因为hello 在函数定义之后被调用(因此没有名称错误)!

如果您将hello() 放在定义之间,您将再次收到错误:

def hello():
    print("hello world")
    python()

hello()

def python():
    print("testing main")

给予:

hello world 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-21db1c6bfff8> in <module>()
      3     python()
      4 
----> 5 hello()
      6 
      7 def python():

<ipython-input-7-21db1c6bfff8> in hello()
      1 def hello():
      2     print("hello world")
----> 3     python()
      4 
      5 hello()

NameError: name 'python' is not defined

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2017-01-12
    • 1970-01-01
    • 2016-05-07
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多