ADVERTISEMENT
ADVERTISEMENT

Python program to call methods from main() method

Here, we will write a Python program to call methods from the main() method.
Submitted by Shivang Yadav, on March 18, 2021

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

Python Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.