Python Forum
__name__ and __main__ in functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__name__ and __main__ in functions
#1
Here's a short program:
lwords = ["a", "an", "and", "as", "at", "but", "for", "how", "if", "in", "of", "off", "nor", "or", "so", "the", "to", "up", "via", "with", "yet"]

def make_title(sT):
    rlist = []
    rs = ""
    for word in sT.split():
        if not word.isupper() and word not in lwords:
            word = word.title()
        rlist.append(word)
        
    if not rlist[0].isupper():
        rlist[0].title()
    if not rlist[-1].isupper():
        rlist[-1] = rlist[-1].title()
        
    for word in rlist:
        rs += " " + word
        
    rs = rs.strip()
    return rs

def main():
    sT = input("Enter the title: ")
    print(make_title(sT))
          
if __name__ == "__main__": #infinite loop results with L26 and L27 uncommented.
    main()
I can follow the logic until the last two lines. What do __name__ and __main__ do with regard to [this] function[s]?

Actually, I guess main() is a function defined to contain the main function so no mystery there. What is this __name__, though?

Thanks!
Reply


Messages In This Thread
__name__ and __main__ in functions - by Mark17 - Oct-11-2023, 06:09 PM
RE: __name__ and __main__ in functions - by buran - Oct-11-2023, 06:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  question about if __name__=="__main__" aaander 6 1,668 Nov-13-2022, 09:06 PM
Last Post: deanhystad
  if __name__=='__main__' albin2005 3 2,311 Sep-07-2021, 09:21 AM
Last Post: albin2005
  Questions about __name__ SheeppOSU 3 2,439 Jul-11-2019, 12:51 PM
Last Post: DeaD_EyE
  ModuleNotFoundError: No module named '__main__.vtt'; '__main__' is not a package MM2018 26 17,734 Oct-12-2018, 05:40 AM
Last Post: MM2018
  Why do we use __main__ when we can call a function directly? Dave7Swift 5 4,062 Jun-04-2018, 05:01 AM
Last Post: Dave7Swift
  Problem with __main__ sylas 2 2,951 Mar-30-2018, 07:51 PM
Last Post: snippsat
  [split] can't find '__main__' module Blue Dog 1 9,658 Oct-18-2016, 12:23 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020