Python function calling order

Viewed 7305

How does Python "read in" a program when you run it? For example, I don't understand why there wouldn't be a NameError: name 'cough' is not defined in the below code:

def main():
    for i in range(3):
        cough()


def cough():
    print('cough')


if __name__ == '__main__':
    main()

Basically, my question can also be stated as why do the above and below programs output the same thing:

def cough():
    print('cough')


def main():
    for i in range(3):
        cough()


if __name__ == '__main__':
    main()
5 Answers
Related