Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I've seen some code samples and tutorials that use

def main(): # my code here 

if __name__ == "__main__":

main()

But why? Is there any reason not to define your functions at the top of the file, then just write code under it? ie

def my_function() # my code here def my_function_two() # my code here # some code # call function # print(something)

I just wonder if there is any rhyme to the main?

1 Answer

0 votes
by (106k points)
edited by

Yes, you can execute your code without defining any function at the top but for that, you need to import the script as a module.

You just need to write the following mentioned code at the top of your code:-

import module; 

module.main()

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Feb 4, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Jan 24, 2020 in Java by angadmishra (6.5k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...