Python函数调用不同?

dha*_*ana 2 python methods function python-2.7

当我使用以下方式调用该函数时,

method_a()
def method_a():

    print "I am method A"
Run Code Online (Sandbox Code Playgroud)

我从上面的代码中得到一个错误,

Traceback (most recent call last):
  File "class1.py", line 1, in <module>
    method_a()
NameError: name 'method_a' is not defined
Run Code Online (Sandbox Code Playgroud)

当我按下面的方式调用函数时,

def method_a():

    print "I am method A"

method_a()
Run Code Online (Sandbox Code Playgroud)

我得到了正确的输出,没有上述代码中的任何错误

I am method A 
Run Code Online (Sandbox Code Playgroud)

上面两个代码有什么区别?谢谢..

pts*_*pts 10

Python从上到下执行源文件.直到def method_a(): ...执行该方法才创建它.