Python program to call methods from main() method

Here, we will write a Python program to call methods from the main() method. By Shivang Yadav Last updated : January 05, 2024

Basically, there is no specific main() with special functionality as we see in C / C++.

The main() functionality will not be the first block of Python code to be executed. If the programmer wishes to, it can be done using the __name__ variable that is set to the name value by interpreter. If the file is directly being run __name__ will have __main__ value. If it is imported to a file __name__ will have a calling module name.

Python program to call main() as a function

# Python program to call main() as a function

# function - main() 
def main():
    hi()
    
# function - hi()
def hi():
    print("Hi")

# calling main function 
main()

Output:

Hi

Python program to call main() using __name__

# Python program to call main() using __name__ 

# function - main() 
def main():
    hi()
    
# function - hi()
def hi():
    print("Hi")

# calling main function using __name__ 
if __name__=="__main__":
    main()

Output:

Hi

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.