Why we need a main script in Python

Why we need a main script? We will see how it comes to our help in the later part when we write modules but first of all we can call any function inside it and never care where we declare that function. Let us see an example first




#!/usr/bin/python3

def name():
    print("this will output name.")

# now to execute this function properly, we need to call it after we defined it

# like:

name()

# the output is : this will output name.

# remember we can not call it before the function definition
# if we try to do that this error message will show up in your console

# Traceback (most recent call last):
#   File "/home/sanjib/PycharmProjects/FirstProject/primary/main_script.py", line 8, in 
#     name()
# NameError: name 'name' is not defined

# to solve this problem we can create a main() function like this:


# if we run our program it gives us a nice output like this:

# This is main function.

# now we can call our name() function inside it and the output is:

# This is main function.
# this will output name.

# let us define another function now


def main():
    print("This is main function.")
    name()
    age()


def age():
    print("this is age.")



if __name__ == "__main__":main()

# and we have an output like this:

# This is main function.
# this will output name.
# this is age.



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s