should function be defined before it is used in python?

前端 未结 3 1877
不思量自难忘°
不思量自难忘° 2021-01-24 18:45

should the functions be defined before it is used? but why the following code works:

def main():
    dog()

def dog():
    print(\"This is a dog.\")

if __name__         


        
3条回答
  •  醉酒成梦
    2021-01-24 18:50

    Actually it's not (defined after it's called). This script will do the following:

    • create function and assign it to "main"
    • create function and assign it to "dog"
    • call "main"

    At that point dog is already known in global scope and main can call it.

提交回复
热议问题