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
#2
This link gives a description
https://www.freecodecamp.org/news/if-nam...n-example/
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
https://stackoverflow.com/q/419163/4046632
https://realpython.com/if-name-main-python/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
You've authored 100 posts and this is unfamiliar to you? Bizarre! Using Jupyter Notebook must really be a completely different experience than writing Python the "normal" way.

Welcome to the wonderful world of dunders. Just like "normal" attributes, dunders come in two flavors: variables and methods. The double underscores surrounding the name indicate that Python things this thing is special. If you've written many classes you are probably familiar with dunder methods like __str__() and __repr__(). Python calls these methods when it wants to print an instance of your class as a pretty string (__str__) or a more informative representation (__repr__). Sometimes called "magic methods", the only magic is that the str() knows to call __str__().

You can read about dunder methods here:

https://www.geeksforgeeks.org/dunder-mag...ds-python/

There are also dunder variables. Like the methods, the double underscores on both ends of these variables mark them as being important to Python. I've never seen them called magic variables, but they are automatically assigned values, as if by magic. It is harder to find a list of dunder variables, but they are sprinkled about in this document.

https://docs.python.org/3/reference/data...ml#modules

Unfortunately the same document talks about dunder methods as well, and there are a log of double underscores. Over 1000!

According to said document:
Quote:__file__
The pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter. For extension modules loaded dynamically from a shared library, it’s the pathname of the shared library file.
Strangely enough, there is no mention of the most common use of the __file__ module attribute nor any mention that __file__ is set to "__main__". To learn about that, you need to look here:

https://docs.python.org/3/library/__main__.html
Quote:What is the “top-level code environment”?
__main__ is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It’s “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.

That's the long of it. The short of it is that __file__ is set to "__main__" when the file is the "top-level code environment". Programmers use this to prevent some code from executing when the file is imported (__name__ will not be "__main__"). In your particular example, the programmer does not want to call main() when the module is imported.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about if __name__=="__main__" aaander 6 1,372 Nov-13-2022, 09:06 PM
Last Post: deanhystad
  if __name__=='__main__' albin2005 3 2,158 Sep-07-2021, 09:21 AM
Last Post: albin2005
  Questions about __name__ SheeppOSU 3 2,296 Jul-11-2019, 12:51 PM
Last Post: DeaD_EyE
  ModuleNotFoundError: No module named '__main__.vtt'; '__main__' is not a package MM2018 26 16,941 Oct-12-2018, 05:40 AM
Last Post: MM2018
  Why do we use __main__ when we can call a function directly? Dave7Swift 5 3,869 Jun-04-2018, 05:01 AM
Last Post: Dave7Swift
  Problem with __main__ sylas 2 2,781 Mar-30-2018, 07:51 PM
Last Post: snippsat
  [split] can't find '__main__' module Blue Dog 1 9,489 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